Arrays in JavaScript - GeeksforGeeks

21 Jul, 2021
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): 
javascript
// Initializing while declaringvar house = ["1BHK", "2BHK", "3BHK", "4BHK"];
Example (for Method 2): 
javascript
// Initializing while declaring// Creates an array having elements 10, 20, 30, 40, 50var house = new Array(10, 20, 30, 40, 50);  //Creates an array of 5 undefined elementsvar house1 = new Array(5);  //Creates an array with element 1BHKvar 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.  
javascript
// Creates an array of 4 undefined elementsvar house1 = new Array(4);  // Now assign valueshouse1[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:  
javascript
// 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: 
javascript
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: 
javascript
var house = ["1BHK", 25000, "2BHK", 50000, "Rent", true];  //len contains the length of the arrayvar 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:  
Functions Part 1
Functions Part 2
Functions Part 3
This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
Shivam_k
gabaa406
himanimishra
javascript-array
javascript-basics
JavaScript-Misc
GBlog
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, ...
Socket Programming in C/C++
Top 10 Front End Developer Skills That You Need in 2022
DSA Sheet by Love Babbar
Must Do Coding Questions for Product Based Companies
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
File uploading in React.js