;

Javascript Array - values() Method


Tutorialsrack 05/11/2022 Jquery Javascript

In this article, you will learn about the Javascript Array built-in method Array.prototype.values(). How does this method work in javascript? 

Array.prototype.values()

The Array.prototype.values() method returns a new array iterator that contains the keys for each index in the calling array.

Syntax
array.values();

This method takes no parameter. And This method does not change the original array.

If you used this method on sparse arrays, then this method iterates empty slots as if they have the value undefined.

Here are some examples of Array.prototype.values() method:

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

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

// expected output: "a"
// expected output: "b"
// expected output: "c"



// Using values() method on sparse array
const arr = ['a', 'b', ,'c'];
const iterator = arr.values();

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

// expected output: "a"
// expected output: "b"
// expected output: undefined
// expected output: "c"

I hope this article will help you to understand the javascript Array built-in method Array.prototype.values()

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags