Categories
JavaScript Answers

How to iterate through property names of JavaScript object?

Spread the love

Sometimes, we want to iterate through property names of JavaScript object.

In this article, we’ll look at how to iterate through property names of JavaScript object.

How to iterate through property names of JavaScript object?

To iterate through property names of JavaScript object, we can use the Object.keys method.

For instance, we write

Object.keys(obj).forEach((key) => {
  console.log(key);
});

to call Object.keys with obj to return an array of string property keys in obj.

Then we call forEach with a function that logs the key, which is the property name string to loop through each element returned by Object.keys.

Conclusion

To iterate through property names of JavaScript object, we can use 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.