Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript array.values() Function

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 15 Dec, 2022
Improve Article
Save Article
Like Article

The Javascript array.values() function is an inbuilt function 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 values: It returns a new array iterator object i.e, elements of the given array. 

Examples:  

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

Let’s see the JavaScript program on the array.values() function: 

Example: In this example, we will print the values of an array using the array values() function. 

JavaScript




<script>
    // Input array contain some elements
    var A = [ 'Ram', 'Z', 'k', 'geeksforgeeks' ];
      
    // Here array.values() function is called.
    var 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);
</script>


Output:

Ram
z
k
geeksforgeeks

Application: This array.values() function in JavaScript is used to print the elements of the given array. 
Let’s see the JavaScript program on the array.values() function: 

Example: In this example, we will be using the array values() function with for loop to print the values of an array. 

JavaScript




<script>
    // Input array contain some elements.
    var array = [ 'a', 'gfg', 'c', 'n' ];
      
    // Here array.values() function is called.
    var iterator = array.values();
      
    // Here all the elements of the array is being printed.
    for (let elements of iterator) {
      console.log(elements);
    }
</script>


Output:

a
gfg
c
n

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

Supported Browser:

  • Google Chrome 66 and above
  • Microsoft Edge 12 and above
  • Firefox 60 and above
  • Opera 53 and above
  • Safari 9 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!