JavaScript Array

 15 - Oct - 2021

An array of Javascript stores more than one value. Many programming languages have elements of the same data types in the array but can be of any type in the array of Javascript.

An array is created in three ways.

  • Using an array literal
  • Using the new keyword
  • Using an array constructor

Using an array literal

The syntax of the literals of the arrays is simple. It takes a list of values separated by a comma and encloses them in square brackets.


var arr_name = [element1, element2,..., elementN];


var names = ["Ravi", "Gopal", "Ram"];

Using the new keyword

An array is created with a new keyword. so you can see the following syntax or example for creating an array using the new keyword.


var <array-name> = new Object();


var name = new Array(); //Creating Array Object
name[0] = "Ravi";
name[1] = "Gopal";
name[2] = "Ram";

Using an array constructor

You can create an array with Array constructor syntax using a new keyword.


var <array-name> = new Array(element1, element2, element3,... elementN);


var names = new Array("Ravi","Gopal","Ram");