Declaration of an Array
There are basically two ways to declare an array.
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):
Example (for Method 2):
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.
We can store Numbers, Strings and Boolean in a single array.
Accessing Array Elements
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:
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 email@example.com. 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.
Improved By : Shivam_k