Ways to create an Array in JavaScript

Aniket Kudale
JavaScript Shorts
Published in
1 min readJun 25, 2020

--

Photo by Ilya Pavlov on Unsplash

We all are familiar with Arrays

Let us see some of the ways to create an array in JavaScript.

  1. Array Literal
let array = [1, 2, 3]console.log(array) // [1, 2, 3]

2. Array Constructor

let array = new Array(1 ,2 ,3)console.log(array) // [1, 2, 3]

3. Array.from()

Creates a new, shallow copied Array instance from and array-like or iterable object.

console.log(Array.from('JavaScript'))
// Output: ["J", "a", "v", "a", "S", "c", "r", "i", "p", "t"]

4. Array.of()

This method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.

Array.of(7) // [7]
Array.of(1, 2, 3) // [1, 2, 3]

So these are the ways to create an Array in JavaScript.

Did I miss any other way? Let me know in the comments.

— Aniket Kudale

--

--