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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.