Skip to content
Related Articles

Related Articles

Arrays in JavaScript

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 25 Nov, 2022
Improve Article
Save Article

JavaScript array is a single variable that is used to store different elements. It is often used when we want to store a list of elements and access them by a single variable. Unlike most languages where the array is a reference to the multiple variables, in JavaScript, an array is a single variable that stores multiple elements.

Declaration of an Array: There are basically two ways to declare an array.

Syntax:

let arrayName = [value1, value2, ...]; // Method 1
let arrayName = new Array(); // Method 2

Note: Generally method 1 is preferred over method 2. Let us understand the reason for this.

Example: Initialization of an Array according to method 1.

// Initializing while declaring
var house = ["1BHK", "2BHK", "3BHK", "4BHK"];

Example: Initialization of an Array according to method 2.

javascript




<script>
    // 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("1BHK");
    console.log(house)
    console.log(house1)
    console.log(home)
</script>


As shown in the above example the house contains 5 elements i.e. (10, 20, 30, 40, 50) while the 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.

Output:

[10, 20, 30, 40, 50]
[undefined, undefined, undefined, undefined, undefined]
["1BHK"]

Example: We can also update after initialization.

javascript




<script>
    // Creates an array of 4 undefined elements
    var house1 = new Array(4);
      
    // Now assign values
    house1[0] = "1BHK"
    house1[1] = "2BHK"
    house1[2] = "3BHK"
    house1[3] = "4BHK"
    console.log(house1)
</script>


Output:

["1BHK", "2BHK", "3BHK", "4BHK"]

Example: An array in JavaScript can hold different elements that can store Numbers, Strings, and Boolean in a single array.

javascript




<script>
    // Storing number, boolean, strings in an Array 
    var house = ["1BHK", 25000, "2BHK", 50000, "Rent", true]; 
    console.log(house)
</script>


Output:

["1BHK", 25000, "2BHK", 50000, "Rent", true]

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

javascript




<script>
    var house = ["1BHK", 25000, "2BHK", 50000, "Rent", true];
    console.log(house[0]+" cost= "+house[1]);
    var cost_1BHK = house[1];
    var is_for_rent = house[5];
    console.log("Cost of 1BHK = "+ cost_1BHK);
    console.log("Is house for rent = ")+ is_for_rent;
</script>


Output:

"1BHK cost= 25000"
"Cost of 1BHK = 25000"
"Is house for rent = "

Example: Length property of an Array returns the length of an Array. The length of an Array is always one more than the highest index of an Array.

javascript




<script>
    var house = ["1BHK", 25000, "2BHK", 50000, "Rent", true];
    // len contains the length of the array
    var len = house.length;
    for (var i = 0; i < len; i++)
        console.log(house[i]);//Output 1BHK
</script>


Output:

"1BHK"
25000
"2BHK"
50000
"Rent"
true

For commonly used Array methods refer to the links below.

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array reference article.

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.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!