Javascript Arrays

Javascript Arrays

What is an array?

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

Example : var cars = ["Saab", "Volvo", "BMW"];

Creating an array

The most commonly used syntax for creating an array is:

var arr = ["item1", "item2", "item3",];

Another method is by using the JavaScript Keyword new: var arr = new Array("item1", "item2", "item3",);

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 cars = ["Saab", "Volvo", "BMW"];
cars [0];

Array elements are numbered, starting with zero. We can access an element by its number in square brackets:

Example:

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

Another example:

var fruits = ["Apple", "Orange", "Plum"];

alert( fruits[0] ); // Apple
alert( fruits[1] ); // Orange
alert( fruits[2] ); // Plum

We can also access the Last Array Element

Example

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

Manipulating Arrays

We can replace an element in an array- from the array above,:

fruits[2] = 'Pear'; // now ["Apple", "Orange", "Pear"]

…Or add a new one to the array:

fruits[3] = 'Lemon'; // now ["Apple", "Orange", "Pear", "Lemon"]

The total count of the elements in the array is its length:

var fruits = ["Apple", "Orange", "Plum"];

fruits.length ; // 3

Array methods

Methods that work with the end of the array:

pop : this extracts the last element of the array and returns it:

var fruits = ["Apple", "Orange", "Pear"];

alert( fruits.pop() ); // remove "Pear" and alert it

alert( fruits ); // Apple, Orange

push this appends the element to the end of the array:

var fruits = ["Apple", "Orange"];

fruits.push("Pear");

alert( fruits ); // Apple, Orange, Pear

Methods that work with the beginning of the array:

shift : This method extracts the first element of the array and returns it:

var fruits = ["Apple", "Orange", "Pear"];

alert( fruits.shift() ); // remove Apple and alert it

alert( fruits ); // Orange, Pear

unshift : This method adds the element to the beginning of the array:

var fruits = ["Orange", "Pear"];

fruits.unshift('Apple');

alert( fruits ); // Apple, Orange, Pear

Methods push and unshift can add multiple elements at once:

var fruits = ["Apple"];

fruits.push("Orange", "Peach");
fruits.unshift("Pineapple", "Lemon");

// ["Pineapple", "Lemon", "Apple", "Orange", "Peach"]
alert( fruits );

Check if an value is an array

To check if a value is an array, you use

Array.isArray() method:
console.log(Array.isArray(seas)); // true

The Difference Between Arrays and Objects

In JavaScript, arrays use numbered indexes.

In JavaScript, objects use named indexes.

Summary

In JavaScript, an array is an order list of values. Each value is called an element specified by an index. An array can hold values of mixed types. JavaScript arrays are dynamic. They grow or shrink as needed.