JS Built-ins
← Array133 / 226
ArrayArray.prototype.mapbeginner

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

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

Pass String directly as the map() callback to convert every number to its string equivalent.

Array.prototype.map() β€” type conversion

map can be used to convert every element to a different type. Passing a built-in function such as String directly as the callback is a concise pattern.

Challenge: Implement toStrings(nums) that converts each number to its string representation.

toStrings([1, 2, 3])    // β†’ ['1', '2', '3']
toStrings([0, -5, 100]) // β†’ ['0', '-5', '100']
πŸ’‘ Hints (2)
  • β€’ `String` can be passed directly as the callback: `nums.map(String)`.
  • β€’ Alternatively: `nums.map(n => String(n))` or `nums.map(n => `${n}`)`.