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):
// Initializing while declaring var house = ["1BHK", "2BHK", "3BHK", "4BHK"]; // Initializing after declaring house[0] = "1BHK"; house[0] = "2BHK"; house[0] = "3BHK"; house[0] = "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.
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:
- How to compare two arrays in JavaScript?
- JavaScript | JSON Arrays
- Compute the set difference using JavaScript arrays
- New features of JavaScript Arrays with ES2015
- How to find if two arrays contain any common item in Javascript?
- Converting JavaScript Arrays into CSVs and Vice-Versa
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Loops in JavaScript
Improved By : Shivam_k



