trainingtrains Logo

91-9990449935

 0120-4256464

JavaScript Array

JavaScript array is an object that represents a collection of similar type of elements.

There are 3 ways to construct array in JavaScript

  1. By array literal
  2. By creating instance of Array directly (using new keyword)
  3. By using an Array constructor (using new keyword)

1) JavaScript array literal

The syntax of creating array using array literal is given below:

As you can see, values are contained inside [ ] and separated by , (comma).

Let’s see the simple example of creating and using array in JavaScript.

Test it Now

The .length property returns the length of an array.

Output of the above example


2) JavaScript Array directly (new keyword)

The syntax of creating array directly is given below:

Here, new keyword is used to create instance of array.

Let’s see the example of creating array directly.

Test it Now

Output of the above example


3) JavaScript array constructor (new keyword)

Here, you need to create instance of array by passing arguments in constructor so that we don't have to provide value explicitely.

The example of creating object by array constructor is given below.

Test it Now

Output of the above example

Next TopicJavaScript String