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.