JavaScript Array values() Method

JavaScript Array values() is a built-in method that returns a new Array Iterator object that contains the values for each index in the array. This method enables you to iterate through the values of an array using a for…of loop or other iterator-consuming constructs.

Syntax

array.values()

Example

let NetflixShows = ['Stranger Things', 'Black Mirror',
                    'You', 'The Crown', 'Dark']
 
const it = NetflixShows.values()
console.log(it)

Output

Javascript Array Values Example

Now, we can get the values by a loop through that iterator.

Example 2

let NetflixShows = ['Stranger Things', 'Black Mirror',
                    'You', 'The Crown', 'Dark']
 
const it = NetflixShows.values()

for (const value of it) {
  console.log(value);
}

Output

Array.prototype.values() Tutorial

Example 3

We can also print the iterator’s value using the next() method.

let NetflixShows = ['Stranger Things', 'Black Mirror',
                    'You', 'The Crown', 'Dark']
 
const it = NetflixShows.values()
console.log(it.next().value);
console.log(it.next().value);
console.log(it.next().value);
console.log(it.next().value);
console.log(it.next().value);

Output

Javascript Array Values Example | Array.prototype.values() Tutorial

We can also use the for…of loop to iterate an iterator.

let NetflixShows = ['Stranger Things', 'Black Mirror',
                    'You', 'The Crown', 'Dark']
 
const it = NetflixShows.values()

for (let value of it) {
  console.log(value)
}

Still, the output remains the same.

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.