Home » JavaScript Array values() Method

JavaScript Array values() Method

by Online Tutorials Library

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.

Test it Now

Output:

After iteration, the elements of the array are represented as:

JavaScript Array values() Method

Example2

Another implementation of array values() method using for…of loop.

Test it Now

Output:

The output is shown below:

JavaScript Array values() Method

Example3

An example implemented using next() method.

Test it Now

Output:

JavaScript Array values() Method

Note: If the next() method use exceeds the given array length, it will return an ‘undefined’ value.

Let’s understand through an example:

Example

Test it Now

Output:

JavaScript Array values() Method

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.


Next TopicJavaScript Array

You may also like