JS Built-ins
← Array27 / 226
ArrayArray.ofbeginner

Array.of() β€” create an array from arguments

πŸ“– Usage Example
Array.of(1, 2, 3)  // β†’ [1, 2, 3]
Array.of(7)        // β†’ [7]  (not a sparse array!)

Use Array.of() to create an array from arguments β€” unlike new Array(), a single number is an element.

Array.of()

Array.of() creates a new Array instance from any number of arguments, regardless of their number or type. Every argument becomes an element in the resulting array.

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

createArrayOf(1, 2, 3) // β†’ [1, 2, 3]
createArrayOf(7)       // β†’ [7]
πŸ’‘ Hints (2)
  • β€’ `Array.of(1, 2, 3)` is equivalent to the literal `[1, 2, 3]`.
  • β€’ Spread the rest parameter into `Array.of()` to forward all arguments.