The Array.values()
method is a built-in JavaScript function that returns a new array iterator object. This iterator contains the values of each element in the array in their original order.
Syntax
arr.values();
Parameters
The Array.values()
method does not take any parameters.
Return Value
The method returns a new Array Iterator object containing the values of the array elements.
Example 1: Basic Usage Array values() Method
let arr = [10, 20, 30, 40, 50]; let iterator = arr.values(); for (let value of iterator) { console.log(value); } /* Output: 10 20 30 40 50 */
Example 2: Using the Iterator Manually
let arr = [10, 20, 30]; let iterator = arr.values(); console.log(iterator.next().value); // Output: 10 console.log(iterator.next().value); // Output: 20 console.log(iterator.next().value); // Output: 30 console.log(iterator.next().done); // Output: true
Example 3: Converting an Iterator to an Array
let arr = [10, 20, 30, 40, 50]; let iterator = arr.values(); let arrayFromIterator = Array.from(iterator); console.log(arrayFromIterator); // Output: [ 10, 20, 30, 40, 50 ]
Example 4: Using values()
in a Nested Array
let arr = [[1, 2], [3, 4], [5, 6]]; let iterator = arr.values(); for (let value of iterator) { console.log(value); } // Output: // [1, 2] // [3, 4] // [5, 6]
Example 5: Combining with Destructuring
let arr = [10, 20, 30]; let [first, second, third] = arr.values(); console.log(first); // Output: 10 console.log(second); // Output: 20 console.log(third); // Output: 30
Supported Browsers
Browser | Support |
---|---|
Chrome | 66+ |
Firefox | 60+ |
Safari | 11.1+ |
Edge | 79+ |
Opera | 53+ |
Internet Explorer | Not Supported |