JavaScript Array values() MethodThe 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. SyntaxParameterIt does not hold any parameter as such. ReturnIt creates and returns a newly created array iterator object. JavaScript Array values() Method ExampleLet's discuss some examples to understand better. Example1 Here's an example implemented using for...of loop. Test it NowOutput: After iteration, the elements of the array are represented as: Example2 Another implementation of array values() method using for...of loop. Test it NowOutput: The output is shown below: Example3 An example implemented using next() method. Test it NowOutput: 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 NowOutput: 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
|