How to Extract Object Keys in JavaScript?

Estimated read time 1 min read

You can extract the keys of an object in JavaScript by using the Object.keys() method.

Here’s an example:

var obj = {
  name: "John",
  age: 30,
  city: "New York"
};
var keys = Object.keys(obj);
console.log(keys);

In this example, the Object.keys() method is used to extract the keys of the object stored in the obj variable. The resulting array of keys is stored in the keys variable, which can be logged to the console or used elsewhere in your code.

The Object.keys() method returns an array of the object’s own enumerable properties. This means that properties that are not enumerable, such as properties inherited from the object’s prototype, will not be included in the array.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply