JavaScript Array values() Method
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();Return value:
- It returns a new array iterator object i.e., elements of the given array.
Example 1: Printing values of array using array values() method
Here, we will print the values of an array using the array values() method.
// Input array contain some elements
let A = ['Ram', 'Z', 'k', 'geeksforgeeks'];
// Here array.values() method is called.
let iterator = A.values();
// All the elements of the array the array
// is being printed.
console.log(iterator.next().value);
console.log(iterator.next().value);
console.log(iterator.next().value);
console.log(iterator.next().value);
Output
Ram Z k geeksforgeeks
Explanation:
- The
values()method is called on the arrayA. - An iterator object
iteratoris obtained from the array. - Each call to
iterator.next()returns an object with avalueproperty containing the next element in the array. - The
valueproperty of each iterator’s result object is logged to the console to print each element of the array sequentially.
Example 2: array values() method with for loop
Here, we will be using the array values() method with for loop to print the values of an array.
// Input array contain some elements.
let array = ['a', 'gfg', 'c', 'n'];
// Here array.values() method is called.
let iterator = array.values();
// Here all the elements of the array is being printed.
for (let elements of iterator) {
console.log(elements);
}
Output
a gfg c n
Explanation:
- The array
arraycontains elements'a','gfg','c', and'n'. - An iterator
iteratoris obtained from the array using thevalues()method. - The
for...ofloop iterates over the elements returned by the iterator. - Each element
elementsis logged to the console. - The output will be
'a','gfg','c', and'n'.
Example 3: Printing elements of array with holes using array values() method
Here, we have used value() method in Arrays with Holes.
let array = ["A", "B", , "C", "D"];
let iterator = array.values();
for (let value of iterator) {
console.log(value);
}
Output
A B undefined C D
Explanation:
- The array
arraycontains elements"A","B", an empty slot,"C", and"D". - An iterator
iteratoris obtained from the array using thevalues()method. - The
for...ofloop iterates over the elements returned by the iterator. - Each element
valueis logged to the console. - The output will be
"A","B","C", and"D". The empty slot will not produce any output.
We have a complete list of JavaScript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browser:
JavaScript Array values() Method- FAQ’s
What does Array.prototype.values() do in JavaScript?
Array.prototype.values() returns an iterator object containing the values of an array, allowing sequential access to each element’s value via the iterator’s next() method.
Does values() modify the original array?
No, the values() method in JavaScript does not modify the original array. It returns an iterator object that allows sequential access to the values of the array, but it does not change the array itself.
When should I use JavaScript values() over other iteration methods?
Use values() when you need to iterate over array values sequentially without modifying the array or accessing indices. It’s handy for simple value iteration and works well with for…of loops.
Does values() work with sparse arrays or arrays with holes?
Yes, values() works with sparse arrays and handles array elements with undefined or uninitialized values as expected, iterating over defined elements only.
Can values() be used to modify or delete array elements?
No, values() only provides read-only access to array values via an iterator. It does not allow modification, deletion, or addition of elements.

