JavaScript Arrays JAVASCRIPT

JavaScript Arrays  

JavaScript Arrays

JavaScript Arrays

JavaScript arrays are used to store multiple values in a single variable.

An array is a special variable, which can hold more than one value at a time.

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

var car1 = "Tesla";
var car2 = "Volvo";
var car3 = "BMW";

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The solution is an array!

An array can hold many values under a single name, and you can access the values by referring to an index number.

Creating an Array

Using an array literal is the easiest way to create a JavaScript Array.

Syntax:

var array_name = [item1item2, ...];      

Spaces and line breaks are not important. A declaration can span multiple lines.

var cars = [
  "Tesla",
  "Volvo",
  "BMW"
];

 

Example

var cars = ["Tesla", "Volvo", "BMW"];

Using the JavaScript Keyword new

The following example also creates an Array, and assigns values to it.

Example

var cars = new Array("Tesla", "Volvo", "BMW");

Access the Elements of an Array

You access an array element by referring to the index number.

This statement accesses the value of the first element in cars.

var name = cars[0];

Example

var cars = ["Tesla", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars[0];

Changing an Array Element

This statement changes the value of the first element in cars:

cars[0] = "TATA;

 

Example

var cars = ["Tesla", "Volvo", "BMW"];
cars[1] = "TATA";
document.getElementById("demo").innerHTML = cars;

The length Property

The length property of an array returns the length of an array (the number of array elements). The length property is always one more than the highest array index. Because array indexing starts from 0.

Example

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.length;   // the length of fruits is 4

Accessing the Last Array Element

The length property is always one more than the highest array index. So get the last element by subtracting 1 in it.

Example

fruits = ["Banana", "Orange", "Apple", "Mango"];
var last = fruits[fruits.length - 1];

Looping Array Elements

The safest way to loop through an array is by using a for loop.

Example

var fruits, text, fLen, i;
fruits = ["Banana", "Orange", "Apple", "Mango"];
fLen = fruits.length;

text = "<ul>";
for (i = 0; i < fLen; i++) {
  text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";

Adding Array Elements

The easiest way to add a new element to an array is by using the push() method:

Example

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon");    // adds a new element (Lemon) to fruits

New element can also be added to an array using the length property:

Example

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[fruits.length] = "Lemon";    // adds a new element (Lemon) to fruits

WARNING !

Adding elements with high indexes can create undefined "holes" in an array:

Example

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[6] = "Lemon";    // adds a new element (Lemon) to fruits

 

Associative Arrays

Many programming languages support arrays with named indexes. Arrays with named indexes are called associative arrays (or hashes). JavaScript does not support arrays with named indexes.

In JavaScript, arrays always use numbered indexes.  

Example

var person = [];
person[0] = "Deepak";
person[1] = "Chahar";
person[2] = 46;
var x = person.length;     // person.length will return 3
var y = person[0];         // person[0] will return "John"

Download free E-book of JAVASCRIPT


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries