Declaration of an Array
There are basically two ways to declare an array.
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 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:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.