Open In App

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. 

Example:  



Input: A = ['a', 'b', 'c', 'd']
Output: a, b, c, d
Explanation:
Here as we see that input array contain some
elements and in output same elements get printed.

Array values() Method Syntax

arr.values();

Array values() Method Return values

It returns a new array iterator object i.e., elements of the given array. 

Array values() Method Examples

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:

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:

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:

We have a complete list of JavaScript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browser:


Article Tags :