JavaScript Array

An array is an object that can store multiple values at once.

Suppose we need to record the age of 5 students. Instead of creating 5 separate variables, we can simply create an array:

Array of 5 elements
Array of 5 elements


Create an Array

We can create an array by placing elements inside an array literal [], separated by commas. For example,

const numbers = [10, 30, 40, 60, 80]

Here,

  • numbers - name of the array
  • [10, 30, 40, 60, 80] - elements of the array
Examples of JavaScript Array
// empty array
const emptyArray = [];

// array of strings
const actions = [ 'eat', 'work', 'sleep'];

// array with mixed data types
const mixedArray = ['work', 1, true];

Access Elements of an Array

Each element of an array is associated with a number called an index. The index specifies the position of the element inside the array.

For the array,

let numbers = [10, 30, 40, 60, 80]

Here is the indexing of each element:

Index of Array Elements
Index of Array Elements

We can use an array index to access the elements of the array.

  • numbers[0] - access the first element 1.
  • numbers[2] - access the third element 3.

Let's look at an example.

let numbers = [10, 30, 40, 60, 80]

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

// access third element
console.log(numbers[2]);

Output

10
40

Note: The index of arrays starts with 0, not 1.


Add Element to an Array

We can add elements to an array using built-in methods like push() and unshift().

  • The push() method adds an element at the end of the array.
let dailyActivities = ['eat', 'sleep'];

// add an element at the end
dailyActivities.push('exercise');

console.log(dailyActivities);

Output

[ 'eat', 'sleep', 'exercise' ]
  • The unshift() method adds an element at the beginning of the array.
let dailyActivities = ['eat', 'sleep'];

// add an element at the start
dailyActivities.unshift('work'); 

console.log(dailyActivities);

Output

[ 'work', 'eat', 'sleep' ]

To learn more, visit Array push() and Array unshift().


Change the Elements of an Array

We can add or change elements by accessing the index value. For example,

let dailyActivities = [ 'eat', 'sleep'];

// insert in index 1
dailyActivities[1] = 'exercise';

console.log(dailyActivities);

Output

[ 'eat', 'exercise' ]

Here, we changed the array element using the array index. The element sleep is replaced by exercise as we inserted the element in index 1.


Remove Elements from an Array

We can remove an element from any specified index of an array using the splice() method.

let numbers = [1, 2, 3, 4, 5];

// remove one element at the index 2 numbers.splice(2, 1);
console.log(numbers);

Output

[ 1, 2, 4, 5 ]

In this example, we removed one element at index 2 using the splice() method.

To learn more, visit JavaScirpt Array splice().


Length of an Array

We can find the length of an array using the length property. For example,

const dailyActivities = [ 'eat', 'sleep'];

// return the length of array
console.log(dailyActivities.length);

Output

2

Array Methods

In JavaScript, there are various methods available that make it easier to perform useful operations with arrays. Some commonly used array methods in JavaScript are:

Method Description
concat() Joins two or more arrays and returns a result.
indexOf() Searches an element of an array and returns its position.
find() Returns the first value of an array element that passes a test.
findIndex() Returns the first index of an array element that passes a test.
forEach() Calls a function for each element.
includes() Checks if an array contains a specified element.
sort() Sorts the elements alphabetically in strings and in ascending order.
slice() Selects the part of an array and returns the new array.
splice() Removes or replaces existing elements and/or adds new elements.

To learn more, visit JavaScript Array Methods.


More on Javascript Array

Create an array using the new keyword

We can create an array using JavaScript's new keyword. For example,

const array2 = new Array("eat", "sleep");

In the above example, we created an array with two elements.

Remove elements from an array using pop() and shift()

We can remove an element from an array using built-in methods like pop() and shift().

  • pop() to remove the last element
let dailyActivities = ['work', 'eat', 'sleep', 'exercise'];

// remove the last element dailyActivities.pop();
console.log(dailyActivities);

Output

[ 'work', 'eat', 'sleep' ]
  • shift() to remove the first element
let dailyActivities = ['work', 'eat', 'sleep', 'exercise'];

// remove the first element dailyActivities.shift();
console.log(dailyActivities);

Output

[ 'eat', 'sleep', 'exercise' ]

To learn more, visit Array pop() and Array shift().

Relationship between arrays and objects in JavaScript

In JavaScript, arrays are objects, with numbered indexes.

Since arrays are objects, the array elements are stored by reference. Hence, when an array value is copied, any change in the copied array will also reflect in the original array. For example,

let arr = ['h', 'e'];
let arr1 = arr;
arr1.push('l');

console.log(arr);
console.log(arr1); 

Output

[ 'h', 'e', 'l' ]
[ 'h', 'e', 'l' ]

Here, we modified the copied array arr1 which also modified the original array arr.


Also Read:

Did you find this article helpful?