JS Built-ins
ArrayArraybeginner

Array() β€” create array with a given length

πŸ“– Usage Example
const sparse = new Array(3)
sparse.length  // β†’ 3
sparse         // β†’ [ <3 empty items> ]

Pass a single number to new Array(n) to create a sparse array with a preset length.

Array constructor β€” with a length argument

When the Array constructor receives a single numeric argument, it creates a sparse array with that many empty slots. The array has the correct length but no actual element values.

Challenge: Implement createArrayOfLength that returns a sparse array of length n.

createArrayOfLength(3)  // β†’ [ <3 empty slots> ] (length === 3)
createArrayOfLength(0)  // β†’ [] (length === 0)
πŸ’‘ Hints (2)
  • β€’ `new Array(n)` creates a sparse array β€” it has a `length` but the slots are empty.
  • β€’ This differs from `new Array(1, 2, 3)` which creates `[1, 2, 3]`.