JS Built-ins
← Array118 / 226
ArrayArray.prototype.keysbeginner

Array.prototype.keys() β€” count the keys

πŸ“– Usage Example
const arr = ['x', 'y', 'z']
[...arr.keys()].length  // β†’ 3  (same as arr.length)

Spreading keys() into an array lets you count or work with the set of indices.

Array.prototype.keys() β€” number of keys

The number of keys yielded by arr.keys() is always the same as arr.length. This exercise makes that relationship explicit.

Challenge: Implement countKeys(arr) that returns the number of keys in the array (i.e. how many indices exist).

countKeys([10, 20, 30]) // β†’ 3
countKeys([])           // β†’ 0
πŸ’‘ Hints (2)
  • β€’ The count of keys always equals `arr.length`.
  • β€’ Convert with `Array.from()` then access `.length`, or just use `arr.length` directly.