Skip to content
Related Articles

Related Articles

Arrays in JavaScript
  • Difficulty Level : Basic
  • Last Updated : 10 May, 2020

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):






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


Example (for Method 2):




// 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");


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.

We can also update after initialization.




// Creates an array of 4 undefined elements
var house1 = new Array(4);
  
// Now assign values
house1[0] = "1BHK"
house1[1] = "2BHK"
house1[2] = "3BHK"
house1[3] = "4BHK"


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




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


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




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);


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:




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]);


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.

geeks-digest-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :