Categories
JavaScript Answers

How to get object property name with JavaScript?

Spread the love

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

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

How to get object property name with JavaScript?

To get object property name with JavaScript, we use the Object.keys method.

For instance, we write

const result = Object.keys(myVar);
console.log(result[0]);

to call Object.keys with myVar to return an array of property key strings in the myVar object.

Then we get the first key from the array with result[0].

Conclusion

To get object property name with JavaScript, we use 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 *