The values() method of the Array class in JavaScript returns a new array iterator object that contains the values for each index in the array.
The syntax of Array.values() method is:
values()
This values() method of Array returns a new array iterator object that contains the values for each index in the array.
Array.values() returns value of type Array Iterator
.
In JavaScript, we can use the values() method to iterate over array values.
For example,
const arr = [1, 2, 3, 4, 5];
const valuesIterator = arr.values();
for (const value of valuesIterator) {
console.log(value);
}
1 2 3 4 5
We can also use the values() method on an array of strings to iterate over string values.
For example,
const strArr = ['apple', 'banana', 'cherry', 'date'];
const valuesIterator = strArr.values();
for (const value of valuesIterator) {
console.log(value);
}
apple banana cherry date
In this JavaScript tutorial, we learned about values() method of Array: the syntax and few working examples with output and detailed explanation for each example.