Categories
JavaScript Answers

How to iterate array keys in JavaScript?

Spread the love

Sometimes, we want to iterate array keys in JavaScript.

In this article, we’ll look at how to iterate array keys in JavaScript.

How to iterate array keys in JavaScript?

To iterate array keys in JavaScript, we can get the keys with the keys method.

For instance, we write

for (const k of Array.from(Array(10).keys())) {
  console.log(k);
}

to get the keys of the Array(10) array as an array of keys with Array.from(Array(10).keys()).

Then we loop through each of them with a for-of loop.

Conclusion

To iterate array keys in JavaScript, we can get the keys with the 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 *