JavaScript array is an object that represents a collection of similar type of elements.
There are 3 ways to construct array in JavaScript
- By array literal
- By creating instance of Array directly (using new keyword)
- By using an Array constructor (using new key
1) JavaScript array literal
Below syntax is creating an array.
var arrayname=[value1,value2.....valueN];
Eg: var cars = ["Hyundai", "Volvo", "BMW"];
2) JavaScript Array directly (new keyword)
The syntax of javascript array using new keyword is as below.
var arrayname=new Array();
Eg: var cars = new Array("Hyundai", "Volvo", "BMW");
3) By using array constructor
Here, you need to create instance of array by passing arguments in
constructor .
Syntax:
var arrayname=new Array("param1","param2","param3")
Eg:
var emp=new Array("Hyundai","Volvo","BMW")