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:

Declaration of an Array in JavaScript
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):
Initialization of an Array(Method 1)
Example (for Method 2):
Initialization of an Array(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.

An array in JavaScript can hold different elements
We can store Numbers, Strings and Boolean in a single array.
Example:
Array of JavaScrit supports all elements

Accessing Array Elements
Array in JavaScript are indexed from 0 so we can access array elements as follows:
Accessing elements in an Array

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:
Length Property of Arrays

Note : All the above examples can be tested by typing them within the script tag of HTML

Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits:http://www.geeksforgeeks.org/arrays-in-javascript/
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at

http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide

rakesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar