Categories
JavaScript Answers

How to get object property name as a string with JavaScript?

Spread the love

Sometimes, we want to get object property name as a string with JavaScript.

In this article, we’ll look at how to get object property name as a string with JavaScript.

How to get object property name as a string with JavaScript?

To get object property name as a string with JavaScript, we call the Object.keys method.

For instance, we write

const person = { firstName: "John", lastName: "Smith", age: "30" };
const listPropertyNames = Object.keys(person);

to call Object.keys with the person to return an array of non-inherited string property keys.

Therefore, listPropertyNames is ['firstName', 'lastName', 'age'].

Conclusion

To get object property name as a string with JavaScript, we call the Object.keys method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *