JavaScript Array values() Method
The values() method creates a new array iterator object that carries the values specified at each array index. We can iterate the array elements via loops or iterator methods.
Syntax
Parameter
It does not hold any parameter as such.
Return
It creates and returns a newly created array iterator object.
JavaScript Array values() Method Example
Let’s discuss some examples to understand better.
Example1
Here’s an example implemented using for…of loop.
Output:
After iteration, the elements of the array are represented as:
Example2
Another implementation of array values() method using for…of loop.
Output:
The output is shown below:
Example3
An example implemented using next() method.
Output:
Note: If the next() method use exceeds the given array length, it will return an ‘undefined’ value.
Let’s understand through an example:
Example
Output:
It is clear that the size of the array was 4, i.e., arr[4]. But next() method was used for 5 times. Thus, the last call to next() had returned an ‘undefined’ value.
Note: The array iterator object carries the array address as its value, and thus depends on the values present in the array, respectively.