JavaScript Array values() Method
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.
- Returns an iterator for accessing array values.
- Does not modify the original array.
- Works with any array, including sparse ones.
- Commonly used with for...of loops.
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);
}
OutputApple
Banana
Cherry
Note => The values() method does not modify the original array. It returns a new array iterator object i.e., elements of the given array.
How the values() Method Works
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:
- value: The next value in the array.
- done: A boolean indicating whether all values have been iterated over (true when the iteration is complete).
JavaScript
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());
Output{ 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:
- The array contains elements
'a'
, 'gfg'
, 'c'
, and 'n'
. - An iterator is obtained from the array using the
values()
method. - The
for...of
loop iterates over the elements returned by the iterator. - Each element
elements
is logged to the console. - The output will be
'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
- The array contains elements
"A"
, "B"
, an empty slot, "C"
, and "D"
. - An iterator is obtained from the array using the
values()
method. - The
for...of
loop iterates over the elements returned by the iterator. - Each element
value
is logged to the console. - The output will be
"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
- An array contains objects with name and age properties.
- values() creates an iterator for the array.
- The for...of loop iterates over the array values (student objects).
- Inside the loop, it logs each student's name to the console.
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
- iterator = arr.values() creates an iterator for the array.
- iterator.next().value gets the first value (1) from the array and logs it.
- It then gets and logs the next values (2, 3, and 4) one by one.
- Each next().value call retrieves the next item in the array.
For more details follow this article => Javascript Array Complete reference
Conclusion
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.
Similar Reads
JavaScript Array() Constructor The Array() constructor is used to create Array objects and the array constructor can be called with or without a new keyword, both can create a new Array.Syntax:new Array(Value1, Value2, ...);new Array(ArrayLength);Array(Value1, Value2, ...);Array(ArrayLength);Parameters: ValueN: An array initializ
2 min read
JavaScript Array constructor Property The JavaScript Array constructor property is used to return the constructor function for an array object. It only returns the reference of the function and does not return the name of the function. In JavaScript arrays, it returns the function Array(){ [native code] }.Syntax: array.constructorReturn
2 min read
JavaScript Array length JavaScript array length property is used to set or return the number of elements in an array. JavaScriptlet a = ["js", "html", "gfg"]; console.log(a.length);Output3 Setting the Length of an ArrayThe length property can also be used to set the length of an array. It allows you to truncate or extend t
2 min read
JavaScript Array from() Method The JavaScript Array from() method returns an Array object from any object with a length property or an iterable object. Syntax : Array.from(object, mapFunction, thisValue)Parameters:object: This Parameter is required to specify the object to convert to an array.mapFunction: This Parameter specifies
3 min read
JavaScript Array isArray() Method The isArray() method in JavaScript is used to determine whether a given value is an array or not. This method returns true if the argument passed is an array else it returns false.Syntax:Array.isArray(obj);Parameters:obj: This parameter holds the object that will be tested.Return value:This function
3 min read
JavaScript Array of() Method The Javascript array.of() method is an inbuilt method in JavaScript that creates a new array instance with variables present as the argument of the method.Syntax:Array.of(element0, element1, ....)Parameters: Parameters present are element0, element1, .... which are basically an element for which the
2 min read
Javascript Array at() Method The JavaScript Array at() method takes an integer value (index) as a parameter and returns the element of that index. It allows positive and negative integers. For the negative integer, it counts back from the last element in the array.Syntax:at(index);Parameter: This method accepts one parameter th
3 min read
JavaScript Array concat() Method The concat() method concatenates (joins) two or more arrays. It returns a new array, containing the joined arrays. This method is useful for combining arrays without modifying the originals.Syntax:let newArray1 = oldArray.concat()let newArray2 = oldArray.concat(value0)let newArray3 = oldArray.concat
3 min read
JavaScript Array copyWithin() Method The Javascript Array.copyWithin() method considers an array first and then copies part of an array to the same array itself and returns it, without modifying its size but yet the modified data whatever user wishes to have in another's place i.e, copies array element of an array within the same array
3 min read
JavaScript Array entries() Method The entries() method in JavaScript is used to create an iterator that returns key/value pairs for each index in the array.It allows iterating over arrays and accessing both the index and value of each element sequentially.Syntax:array.entries()Parameters:This method does not accept any parameters.Re
3 min read