JavaScript Answers

How to Iterate Through Array Keys in JavaScript?

Spread the love

To iterate through array keys in JavaScript, we can use the JavaScript array keys method to get the keys.

For instance, we can write:

const widthRange = []
widthRange[46] = {
  min: 0,
  max: 52
widthRange[66] = {
  min: 52,
  max: 70
widthRange[90] = {
  min: 70,
  max: 94

for (const i of widthRange.keys()) {
  if (widthRange[i]) {

to loop through the keys of the widthRange array with the for-of loop.

We call the keys method to remove all the indexes of the array.

And in the loop body, we check if the item widthRange[i] is truthy before logging its value.

Therefore. we get 46, 66, and 90 from the console log.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.