Arrays in JavaScript

What is an array?

We all know what is an array, it is a collection of similar data elements stored at contiguous memory locations.

How to create an array in javascript?

We can create an array using two ways:

1. By using an array literal: an array literal is [ ]. It is the easiest way to create an array. For example:

const cars = ["Tiago", "Safari", "Indigo"];

2. By using the new keyword:

new is a keyword in javascript used to create an instance of an object that has a constructor function. For example:

const cars = new Array("Tiago", "Safari", "Indigo");

How to access elements of an array?

We can access any element in an array, easily by using array indices.

Now what is an index?

Basically, the index is the location of an item in an array. It starts with 0 and goes to the length of array - 1.

Now, come to our question how to access an array element? For example:

const cars = ["Tiago", "Safari", "Indigo"];

// to access first element 
console.log(cars[0]);

// to access last element
console.log(cars[2]);

Screenshot (117).png

How to add an element to an array?

We have created an array, we know how to access elements of the array. Now let's take a look at how to add elements to an array.

We can add elements to an array by using:

1. By using the push() method: It adds the element at the end of the array. For example:

const cars = ["Tiago", "Safari", "Indigo"];

//adding an element at the end
cars.push("Nexon")

// getting all the elements stored in the array
console.log(cars);

Here is the result,

Screenshot (118).png

2. By using the unshift() method: it adds the element at the beginning of the array. For example:

const cars = ["Tiago", "Safari", "Indigo"];

//adding an element at the beginning
cars.unshift("Tigor");

// getting all the elements stored in the array
console.log(cars);

See the result,

Screenshot (119).png

Suppose, an array has two elements, and if I add an element at the third index then what will happen? Let's take a look.

let country = ["India", "Japan"];

// adding element at index 3
country[3] = "Korea";

// getting the array country
console.log(country);

Screenshot (120).png

The output says empty at index 2. So, if we try to add elements at higher indices, the indices in between will have an empty value.

How to remove elements from the array?

We can remove elements by using:

1. Using the pop() method: It is used to remove the last element from the array. For example:

let country = ["India", "Japan", "Korea"];

// removing last element
country.pop();

// getting the array
console.log(country);

Screenshot (121).png

2. Using the shift() method: It is used to remove the first element from the array. For example:

let country = ["India", "Japan", "Korea"];

// removing first element
country.shift();

// getting the array
console.log(country);

Screenshot (122).png

How to find the length of the array?

Sometimes you'll need to find the length of the given. In that case, you can use the length property to find the number of elements in an array. For example:

let country = ["India", "Japan", "Korea", "USA", "Russia"];

// getting the length of the elements in the array
console.log(country.length);

This basically gives 5 as output.

Screenshot (123).png