JavaScript Arrays

PART-I

Arrays are used to store multiple values in a single variable, rather than declaring separate variables for each value. Each value is called an Element.

In JavaScript, neither the length of the array nor the type of its elements are fixed. That means, there can be an array that stored a number, a string and Boolean values. Since an array's length can change at any time, data can be stored at contiguous memory locations in the array.

Array variables can be referenced using Index. An array index can only be an integer value.

Creating JavaScript Arrays

JavaScript provides two ways of creating an array.

1) Using the Array Constructor

This creates an empty array- scores i.e. the array holds no value.

let scores=new Array();

If the number of elements are known:

let scores=new Array(15);

This creates an array named scores, with length of array=15.

To create an array with elements, a comma-separated list of values can be passed into the constructor.

let scores=new Array (10,15,20,25,30); //Creates an array scores=[10,15,20,25,30]

Few more examples:

let count=new Array(5);  // Creates an array of length 3
let colors=new Array('Yellow'); //Creates an array with one element- Yellow

2) Using Array literal notation

This is the most preferred way to create an array.

let arr = [element1, element2, element3...];

To create an empty array:

let arr=[];

Comma-separated list of elements can also be passed in between the square brackets ([]).

let fruits=['Apple','Mango','Banana']; //Creates an array with three elements

Accessing Array Elements

JavaScript arrays have zero-based index i.e. the index of first element is 0.

To access an array element, index should be specified in square brackets []:

arr[index]

For example:

let countries=['India', 'Singapore', 'Malaysia', 'Thailand'];

console.log(countries[0]); // India
console.log(countries[1]); //Singapore
console.log(countries[3]); // Thailand

Using invalid index number returns undefined. For example, in the above countries array, there are 4 elements, so the index will be in the range 0-3 (length-1). Therefore, below case will result to 'undefined':

console.log(countries[5]); // undefined

Basic Operations on Arrays

1) Length of the array

The length property returns the size of the array i.e the number of elements in it. Example:

let countries=['India', 'Singapore', 'Malaysia', 'Thailand'];
console.log(countries.length); //4

2) Adding an element to the array

a) Add an element to the end of the array: To add an element at the end of the array, push() method is used.

let countries=['India', 'Singapore', 'Malaysia', 'Thailand'];
countries.push('Australia');

console.log(countries);

Output:

['India', 'Singapore', 'Malaysia', 'Thailand', 'Australia']

b) Add an element to the beginning of the array: To add an element to the beginning of the array, unshift() method is used. Example:

let countries=['India', 'Singapore', 'Malaysia', 'Thailand'];
countries.unshift('Australia');

console.log(countries);

Output:

['Australia', 'India', 'Singapore', 'Malaysia', 'Thailand']

3) Removing an element from the array

To remove an element, pop() method is used. This method works on LIFO principle (Last In First Out). That means, the last element of the array will be removed first. Example:

let cities=['Delhi', 'Mumbai', 'Pune', 'Kolkata'];
const lastElement= cities.pop();

console.log(lastElement); // Kolkata

You can also remove an element from the beginning of the array using shift() method. Example:

let cities=['Delhi', 'Mumbai', 'Pune', 'Kolkata'];
const firstElement=cities.shift();

console.log(firstElement); //Delhi

4) Find index of an array element

Index of an array element can be found using indexOf() method. Example:

let countries=['India', 'Singapore', 'Malaysia', 'Thailand'];

console.log(countries.indexOf('Singapore')); // 1

If the element is not found in the array, indexOf() method return -1. Example:

console.log(countries.indexOf('South Africa')); //-1