JS Built-ins
← Array205 / 226
ArrayArray.prototype.toStringintermediate

toString with Nested Arrays

πŸ“– Usage Example
const arr = [1, [2, 3], [4, [5]]]
arr.toString()  // β†’ '1,2,3,4,5'  (recursive flattening)

toString() recursively converts nested arrays, effectively flattening them into the string.

Array.prototype.toString β€” Nested Arrays

When an array contains nested arrays, toString recursively flattens them into the output string. Each nested array is also comma-joined.

Implement nestedToString which returns the string representation of an array that may contain nested arrays.

πŸ’‘ Hints (2)
  • β€’ toString recursively calls toString on each element, including nested arrays
  • β€’ No extra flattening step is needed β€” just call arr.toString()