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.