Javascript Array of()

Introduction

Array of() is used to convert a collection of arguments into an array.

Array.of() can convert the list of arguments into an array.

console.log(Array.of(1, 2, 3, 4));  // [1, 2, 3, 4] 
console.log(Array.of(undefined));   // [undefined] 

The Array.of() method creates a new Array from a variable number of arguments.

Array.of(element0[, element1[, ...[, elementN]]])
  • elementN - Elements used to create the array.

Array.of(7) creates an array with a single element, 7

Array(7) creates an empty array with a length property of 7.

let a = Array.of(7);
console.log(a);// [7] 

a = Array.of(1, 2, 3);//from w  w w .j a v a  2s.  c  o m
console.log(a);// [1, 2, 3]

a = Array(7);
console.log(a);

a = Array(1, 2, 3);
console.log(a);// [1, 2, 3]



PreviousNext

Related