In this article, you will learn about the Javascript Array built-in method Array.prototype.values()
. How does this method work in javascript?
The Array.prototype.values()
method returns a new array iterator that contains the keys for each index in the calling array.
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:
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!
Comments