JS Built-ins
ArrayArraybeginner

Array() β€” create array from multiple arguments

πŸ“– Usage Example
const arr = new Array(1, 2, 3)
arr  // β†’ [1, 2, 3]

Pass multiple arguments to new Array() to create an array pre-filled with those values.

Array constructor β€” with multiple arguments

When the Array constructor receives more than one argument, each argument becomes an element in the resulting array. This is equivalent to an array literal.

Challenge: Implement createArrayFromArgs that accepts any number of numbers and returns them as an array using new Array(...args).

createArrayFromArgs(1, 2, 3) // β†’ [1, 2, 3]
createArrayFromArgs(42)      // β†’ [42]
πŸ’‘ Hints (2)
  • β€’ Spreading the rest parameter into `new Array()` passes each value as a separate argument.
  • β€’ With 2+ numeric args, the constructor stores them as elements, not as a length.