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.
Recommended Posts:
- New features of JavaScript Arrays with ES2015
- Converting JavaScript Arrays into CSVs and Vice-Versa
- JavaScript | JSON Arrays
- How to compare two arrays in JavaScript?
- How to find if two arrays contain any common item in Javascript?
- Compute the set difference using JavaScript arrays
- How to compute union of JavaScript arrays ?
- How to cartesian product of 2 arrays using JavaScript ?
- Find the common elements of more than two JavaScript arrays ?
- How to Merge/Combine Arrays using JavaScript ?
- How to print unique elements from two unsorted arrays using JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
Improved By : Shivam_k

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

