Arrays in JavaScript

In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable. Unlike most languages where array is a reference to the multiple variable, in JavaScript array is a single variable that stores multiple elements.

Declaration of an Array
There are basically two ways to declare an array.
Example:

var House = [ ]; // method 1
var House = new array(); // method 2



But generally method 1 is preferred over the method 2. Let us understand the reason for this.

Initialization of an Array
Example (for Method 1):

filter_none

edit
close

play_arrow

link
brightness_4
code

// Initializing while declaring
var house = ["1BHK", "2BHK", "3BHK", "4BHK"];
  
// Initializing after declaring
house[0] = "1BHK";  
house[0] = "2BHK";
house[0] = "3BHK";
house[0] = "4BHK";

chevron_right


Example (for Method 2):

filter_none

edit
close

play_arrow

link
brightness_4
code

// Initializing while declaring
// Creates an array having elements 10, 20, 30, 40, 50
var house = new Array(10, 20, 30, 40, 50);
  
//Creates an array of 5 undefined elements
var house1 = new Array(5);
  
//Creates an array with element 1BHK
var home = new Array("!BHK");

chevron_right


As shown in above example the house contains 5 elements i.e. (10 , 20, 30, 40, 50) while house1 contains 5 undefined elements instead of having a single element 5. Hence, while working with numbers this method is generally not preferred but it works fine with Strings and Boolean as shown in the example above home contains a single element 1BHK.

An array in JavaScript can hold different elements
We can store Numbers, Strings and Boolean in a single array.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Storing number, boolean, strings in an Array
var house = ["1BHK", 25000, "2BHK", 50000, "Rent", true];

chevron_right


Accessing Array Elements
Array in JavaScript are indexed from 0 so we can access array elements as follows:

filter_none

edit
close

play_arrow

link
brightness_4
code

var house = ["1BHK", 25000, "2BHK", 50000, "Rent", true];
alert(house[0]+" cost= "+house[1]);
var cost_1BHK = house[1];
var is_for_rent = house[5];
alert("Cost of 1BHK = "+ cost_1BHK);
alert("Is house for rent = ")+ is_for_rent);

chevron_right


Length property of an Array
Length property of an Array returns the length of an Array. Length of an Array is always one more than the highest index of an Array.
Example below illustrates the length property of an Array:

filter_none

edit
close

play_arrow

link
brightness_4
code

var house = ["1BHK", 25000, "2BHK", 50000, "Rent", true];
  
//len conatains the length of the array
var len = house.length;
for (var i = 0; i < len; i++)
    alert(house[i]);

chevron_right


Note : All the above examples can be tested by typing them within the script tag of HTML
For commonly used Array methods refer to the links below:

This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Shivam_k