Last Updated : 07 Jun, 2025
JavaScript array.values() is an inbuilt method in JavaScript that is used to return a new array Iterator object that contains the values for each index in the array i.e., it prints all the elements of the array.
Syntax
arr.values();
Now let's understand this with the help of an example
JavaScript
const a = ['Apple', 'Banana', 'Cherry'];
const iterator = a.values();
for (let value of iterator) {
console.log(value);
}
Apple Banana Cherry
How the values() Method WorksNote => The values() method does not modify the original array. It returns a new array iterator object i.e., elements of the given array.
The values() method does not return the values of the array directly. Instead, it returns an iterator object, which is an object that allows you to loop through the array values. The iterator object has a next() method that returns the next value in the array until all the values have been consumed.
How the Iterator Works:
The next() method of the iterator returns an object with two properties:
let a = [10, 20, 30];
let iterator = a.values();
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
{ value: 10, done: false } { value: 20, done: false } { value: 30, done: false } { value: undefined, done: true }Examples of Using the values() Method
Here are the examples using values() methods:
Example 1: Array values() method with for loop.
JavaScript
let a = ['a', 'gfg', 'c', 'n'];
let iterator = a.values();
for (let elements of iterator) {
console.log(elements);
}
In this example:
'a'
, 'gfg'
, 'c'
, and 'n'
.values()
method.for...of
loop iterates over the elements returned by the iterator.elements
is logged to the console.'a'
, 'gfg'
, 'c'
, and 'n'
.Example 2: Printing elements of array with holes using array values() method.
JavaScript
let a = ["A", "B", , "C", "D"];
let iterator = a.values();
for (let value of iterator) {
console.log(value);
}
In this example
"A"
, "B"
, an empty slot, "C"
, and "D"
.values()
method.for...of
loop iterates over the elements returned by the iterator.value
is logged to the console."A"
, "B"
, "C"
, and "D"
. The empty slot will not produce any output.Example 3: Using values() with Object Arrays
When working with arrays of objects, the values() method can be useful for accessing and iterating over the objects' values:
JavaScript
let obj = [
{ name: 'Jiya', age: 18 },
{ name: 'Alia', age: 22 },
{ name: 'Bibita', age: 20 }
];
let iterator = obj.values();
for (let student of iterator) {
console.log(student.name);
}
In this example
Example 4: Using values() with next()
Sometimes, you might prefer using the next() method explicitly
JavaScript
let a = [1, 2, 3, 4];
let iterator = arr.values();
console.log(iterator.next().value);
console.log(iterator.next().value);
console.log(iterator.next().value);
console.log(iterator.next().value);
Output
1
2
3
4
In this example
ConclusionFor more details follow this article => Javascript Array Complete reference
The JavaScript values() method provides a simple way to access and iterate over array values using an iterator. It doesn’t modify the original array and works well with loops like for...of. This method is especially useful for iterating over array values without dealing with indices, making it a clean and efficient choice for array iteration.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4