JavaScript(JS) array method - values

www.igi‮oc.aeditf‬m

The values() method in JavaScript is used to return a new Array Iterator object that contains the values for each index in the array. The Array Iterator object can then be used to loop through the array values using a for...of loop.

Here's an example of using the values() method:

const arr = ['a', 'b', 'c'];
const iterator = arr.values();

for (const value of iterator) {
  console.log(value);
}
// Output:
// "a"
// "b"
// "c"

In the example above, we create an array arr with three values, and then create an iterator object iterator using the values() method. We then use a for...of loop to iterate over the iterator object and print each value in the console.

Note that the values() method is part of the Array Iterator object, which means it can also be used with other methods that return an Array Iterator object, such as the keys() and entries() methods.