JavaScript

JavaScript | array.values()

JavaScript provides a list of methods to perform specific operations on arrays. The array.value() method is used to extract the values of an array with the help of the array iterator object. Additionally, it is also used for printing the array of elements in the console. This article gives a demonstrative view of the array.values() method in JavaScript.

How Does array.values() Method Work in JavaScript?

As discussed above, the array.values() method works on the basis of the iterator object. The iterator object has no ability to store the values present in the array. However, it stores only the index/address of elements of the existing array. The method does not have the capability to change/modify the existing array. The syntax of the array.values() method is described below:

Syntax

array.values()

The values() method does not take any parameters and returns the iterator object of that array.

Example 1

An example is provided that demonstrates the use of the array.values() method in JavaScript:

Code

// An example to use the array.value() in JavaScript

let subject = ["English", "Math", "Chemistry"];

// returns an iterator object that having values of subject

let iterObj = subject.values();

for (let val of iterObj) { // looping through iterator

console.log(val); // display values in subject array

}

The description of the code is given below:

  • First, an array named “subjects” is initialized with the three values English, Math, and Chemistry.
  • After that, subject.values() is utilized to return the values of the array to an iterator object iterObj.
  • Finally, a for loop is utilized that performs iteration over the iterObj to display the values in the array.

Output

The outcome of the code returns the values of the array, including English, Math, and Chemistry, that are stored in the existing subject array.

Example 2

Another example is demonstrated by using the array.values() method in JavaScript.

Code

let sports = ["cricket", "football", "hockey"];

let iteratorObject = sports.values();

console.log(iteratorObject.next().value);

sports[1] = "badminton";

console.log(iteratorObject.next().value);

The description of the code is given below.

  • An array of sports is initialized with cricket, football, and hockey values.
  • After that, an iteratorObject is employed to store the values of the sports array.
  • Furthermore, the next() method is adapted to extract the value of the first element.
  • After that, the first value of the sports array is updated by the assignment of badminton using sports[1]= “badminton”.
  • Finally, display the value by employing the console.log() method.

Output

The output returns two values, cricket and badminton, by utilizing the array.values() method in JavaScript.

Conclusion

The array.values() method is applied to the arrays to get the indexes of the array elements in an iterator object. This iterator object can be used to print the array values as well. Following this post, you have learned to apply the array.values() method in the possible scenarios. The iterator object stores the index only (not the elements), and the values are obtained using the indexes stored in the iterator object. For printing, you may need to apply the for loop or manually print every element.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.