JS Built-ins
← Array136 / 226
ArrayArray.prototype.mapintermediate

Array.prototype.map() β€” parse strings to numbers

πŸ“– Usage Example
const strs = ['1', '2', '3']
strs.map(Number)  // β†’ [1, 2, 3]

Pass Number as the map() callback to parse every string element to a number.

Array.prototype.map() β€” parse numbers

Just as String can be passed directly to map, so can Number. This converts an array of numeric strings into an array of actual numbers in one step.

Challenge: Implement parseNumbers(strs) that converts each string element to a number.

parseNumbers(['1', '2', '3'])  // β†’ [1, 2, 3]
parseNumbers(['10', '-5', '0']) // β†’ [10, -5, 0]
πŸ’‘ Hints (2)
  • β€’ `Number` can be used as a callback directly: `strs.map(Number)`.
  • β€’ Equivalent to `strs.map(s => Number(s))` or `strs.map(s => +s)`.