JavaScript Array
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.
Syntax
var arr_name = [element1, element2,..., elementN];
Example
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.
Syntax
var <array-name> = new Object();
Example
var name = new Array(); //Creating Array Object
name[0] = "Ravi";
name[1] = "Gopal";
name[2] = "Ram";
console.log(name);
Using an array constructor
You can create an array with Array constructor syntax using a new keyword.
Syntax
var <array-name> = new Array(element1, element2, element3,... elementN);
Example
var names = new Array("Ravi","Gopal","Ram");