JS Built-ins
← Array213 / 226
ArrayArray.prototype.valuesbeginner

Iterate with values()

πŸ“– Usage Example
const fruits = ['apple', 'banana', 'cherry']
for (const v of fruits.values()) {
  console.log(v)  // apple, banana, cherry
}

Use values() with for-of to iterate over all element values of an array.

Array.prototype.values β€” for...of

The iterator returned by values() can be consumed with a for...of loop, giving you each element one at a time.

Implement collectValues which uses a for...of loop on arr.values() to collect all elements into a new array.

πŸ’‘ Hints (2)
  • β€’ Use for (const val of arr.values()) and push each val into a result array
  • β€’ arr.values() yields values in index order, just like iterating arr directly