ES6
June 3, 2026 9 min read

Array and methods

HS
HEMANT SINGH
Technical Writer & Educator

JavaScript Arrays

An array is a special type of JavaScript object used to store multiple values in a single variable.

For example:

let arr = [10, 20, 30];

In JavaScript, array indexes start from 0.

Index: 0 1 2
Value: 10 20 30

Empty Array

You can create an empty array using square brackets:

let arr = [];

Array With Elements

You can create an array with multiple elements:

let arr = [10, 20, 30];

An array can contain different types of values:

let arr = [10, "Hello", true, 25.5];

However, in most applications, it is better to keep an array's elements logically related.

Access Whole Array

You can access the complete array using its variable name:

let arr = [10, 20, 30];

console.log(arr);

Output:

[10, 20, 30]

Access Single Element From an Array

You can access an array element using its index.

let arr = [10, 20, 30];

console.log(arr[0]);
console.log(arr[1]);
console.log(arr[2]);

Output:

10
20
30

Remember that array indexing starts from 0.

arr[0] → first element
arr[1] → second element
arr[2] → third element

Length of Array

The length property returns the number of elements in an array.

let arr = [10, 20, 30];

console.log(arr.length);

Output:

3

Example:

let arr = ["HTML", "CSS", "JavaScript", "Laravel"];

console.log(arr.length);

Output:

4

Loops in Arrays

JavaScript provides several ways to loop through an array.

for Loop in Array

A traditional for loop can be used to access every element using its index.

Syntax

for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}

Example

let arr = [10, 20, 30];

for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}

Output:

10
20
30

Here, i represents the array index.

for-in Loop in Array

The for-in loop iterates over the indexes of an array.

Syntax

for (let i in arr) {
console.log(arr[i]);
}

Example

let arr = [10, 20, 30];

for (let i in arr) {
console.log(arr[i]);
}

Output:

10
20
30

In this loop:

i

contains the index.

For example:

i = 0
i = 1
i = 2

for-of Loop in Array

The for-of loop directly gives you the value of each array element.

Syntax

for (let item of arr) {
console.log(item);
}

Example

let arr = [10, 20, 30];

for (let item of arr) {
console.log(item);
}

Output:

10
20
30

Here:

item

contains the actual value.

Difference Between for-in and for-of

let arr = [10, 20, 30];

for (let i in arr) {
console.log(i);
}

Output:

0
1
2

Whereas:

for (let item of arr) {
console.log(item);
}

Output:

10
20
30

Therefore:

for-in → index
for-of → value

JavaScript Array Methods

JavaScript provides many built-in methods for working with arrays.

The following table contains commonly used array methods.


Method

Syntax

Description

push()

arr.push(item1, item2, ...)

Adds one or more items to the end of an array.

pop()

arr.pop()

Removes the last item from an array.

shift()

arr.shift()

Removes the first item from an array.

unshift()

arr.unshift(item1, item2, ...)

Adds one or more items to the beginning of an array.

slice()

arr.slice(startIndex, endIndex)

Returns a new array containing elements from the start index up to, but not including, the end index.

splice()

arr.splice(startIndex, deleteCount, item1, item2, ...)

Adds, removes, or replaces elements in the original array.

forEach()

arr.forEach(function(item, index, array) { })

Executes a function once for each array element.

concat()

arr.concat(value1, value2, ...)

Creates a new array by combining arrays and/or values.

indexOf()

arr.indexOf(searchItem, fromIndex)

Returns the first index where the item is found, otherwise returns -1.

lastIndexOf()

arr.lastIndexOf(searchItem, fromIndex)

Returns the last index where the item is found, otherwise returns -1.

includes()

arr.includes(searchItem, fromIndex)

Returns true if the item exists in the array, otherwise false.

find()

arr.find(function(item, index, array) { return condition; })

Returns the first element that satisfies the condition, otherwise undefined.

findIndex()

arr.findIndex(function(item, index, array) { return condition; })

Returns the index of the first element that satisfies the condition, otherwise -1.

filter()

arr.filter(function(item, index, array) { return condition; })

Returns a new array containing all elements that satisfy the condition.

map()

arr.map(function(item, index, array) { return newValue; })

Creates a new array by transforming each element.

sort()

arr.sort(compareFunction)

Sorts the elements of an array.

reverse()

arr.reverse()

Reverses the order of elements in an array.

join()

arr.join(separator)

Converts array elements into a string using the specified separator.

reduce()

arr.reduce(function(accumulator, item, index, array) { return newValue; }, initialValue)

Reduces all array elements into a single value.

1. push()

The push() method adds one or more elements to the end of an array.

Syntax

arr.push(item1, item2, ...);

Example

let arr = [10, 20];

arr.push(30);

console.log(arr);

Output:

[10, 20, 30]

You can add multiple elements:

arr.push(40, 50);

Output:

[10, 20, 30, 40, 50]

The push() method returns the new length of the array.

let result = arr.push(60);

console.log(result);

2. pop()

The pop() method removes the last element from an array.

Syntax

arr.pop();

Example

let arr = [10, 20, 30];

let item = arr.pop();

console.log(item);
console.log(arr);

Output:

30
[10, 20]

pop() returns the element that was removed.

3. shift()

The shift() method removes the first element from an array.

Syntax

arr.shift();

Example

let arr = [10, 20, 30];

let item = arr.shift();

console.log(item);
console.log(arr);

Output:

10
[20, 30]

4. unshift()

The unshift() method adds one or more elements to the beginning of an array.

Syntax

arr.unshift(item1, item2, ...);

Example

let arr = [30, 40];

arr.unshift(10, 20);

console.log(arr);

Output:

[10, 20, 30, 40]

5. slice()

The slice() method creates a new array by copying elements from a specified range.

Syntax

arr.slice(startIndex, endIndex);

The endIndex is not included.

Example

let arr = [10, 20, 30, 40, 50];

let newArr = arr.slice(1, 4);

console.log(newArr);

Output:

[20, 30, 40]

Here:

slice(1, 4)

Index: 0 1 2 3 4
Value: 10 20 30 40 50
↑ ↑
start end

Elements at indexes 1, 2, and 3 are copied.

The original array is not modified.

console.log(arr);

Output:

[10, 20, 30, 40, 50]

You can also omit the end index:

arr.slice(2);

This returns all elements from index 2 to the end.

6. splice()

The splice() method can remove, add, or replace elements in an array.

Unlike slice(), splice() modifies the original array.

Syntax

arr.splice(startIndex, deleteCount, item1, item2, ...);

Remove elements

let arr = [10, 20, 30, 40];

arr.splice(1, 2);

console.log(arr);

Output:

[10, 40]

Here:

startIndex = 1
deleteCount = 2

So 20 and 30 are removed.

Add elements

To add elements without deleting anything, use 0 as deleteCount.

let arr = [10, 20, 40];

arr.splice(2, 0, 30);

console.log(arr);

Output:

[10, 20, 30, 40]

Replace elements

let arr = [10, 20, 30, 40];

arr.splice(1, 2, 100, 200);

console.log(arr);

Output:

[10, 100, 200, 40]

7. forEach()

The forEach() method executes a function for each element of an array.

Syntax

arr.forEach(function(item, index, array) {

});

Example

let arr = [10, 20, 30];

arr.forEach(function(item, index, array) {
console.log(item);
});

Output:

10
20
30

You can also use the index:

arr.forEach(function(item, index) {
console.log(index, item);
});

Output:

0 10
1 20
2 30

8. concat()

The concat() method creates a new array by combining arrays and/or values.

Syntax

arr.concat(value1, value2, ...);

Example

let arr1 = [10, 20];
let arr2 = [30, 40];

let arr3 = arr1.concat(arr2);

console.log(arr3);

Output:

[10, 20, 30, 40]

You can also combine individual values:

let arr = [10, 20];

let newArr = arr.concat(30, 40);

console.log(newArr);

Output:

[10, 20, 30, 40]

The original array is not modified.

9. indexOf()

The indexOf() method returns the first index where a specified value is found.

Syntax

arr.indexOf(searchItem, fromIndex);

Example

let arr = [10, 20, 30, 20];

console.log(arr.indexOf(20));

Output:

1

If the item is not found, it returns:

-1

Example:

console.log(arr.indexOf(100));

Output:

-1
indexOf() returns -1, not false, when the item is not found.

10. lastIndexOf()

The lastIndexOf() method returns the last index where a specified value is found.

Syntax

arr.lastIndexOf(searchItem, fromIndex);

Example

let arr = [10, 20, 30, 20];

console.log(arr.lastIndexOf(20));

Output:

3

If the item is not found:

-1

11. includes()

The includes() method checks whether an element exists in an array.

Syntax

arr.includes(searchItem, fromIndex);

Example

let arr = [10, 20, 30];

console.log(arr.includes(20));

Output:

true

If the element does not exist:

console.log(arr.includes(100));

Output:

false

12. find()

The find() method returns the first element that satisfies a condition.

Syntax

arr.find(function(item, index, array) {
return condition;
});

Example

let arr = [10, 20, 30, 40];

let result = arr.find(function(item) {
return item > 25;
});

console.log(result);

Output:

30

The method checks:

10 > 25 → false
20 > 25 → false
30 > 25 → true

It stops at the first matching element.

If no element satisfies the condition, it returns:

undefined

13. findIndex()

The findIndex() method returns the index of the first element that satisfies a condition.

Syntax

arr.findIndex(function(item, index, array) {
return condition;
});

Example

let arr = [10, 20, 30, 40];

let index = arr.findIndex(function(item) {
return item > 25;
});

console.log(index);

Output:

2

Because 30 is at index 2.

If no element satisfies the condition, it returns:

-1

14. filter()

The filter() method creates a new array containing all elements that satisfy a condition.

Syntax

arr.filter(function(item, index, array) {
return condition;
});

Example

let arr = [10, 20, 30, 40];

let result = arr.filter(function(item) {
return item > 20;
});

console.log(result);

Output:

[30, 40]

Unlike find(), filter() can return multiple elements.

find() → returns one matching item
filter() → returns an array of matching items

15. map()

The map() method executes a function for every element and creates a new array containing the returned values.

Syntax

arr.map(function(item, index, array) {
return newValue;
});

Example

let arr = [10, 20, 30];

let result = arr.map(function(item) {
return item * 2;
});

console.log(result);

Output:

[20, 40, 60]

The original array remains unchanged.

Arrow Function

The same example can be written more shortly:

let result = arr.map(item => item * 2);

16. sort()

The sort() method sorts the elements of an array.

Syntax

arr.sort(compareFunction);

For strings:

let arr = ["Banana", "Apple", "Mango"];

arr.sort();

console.log(arr);

Output:

["Apple", "Banana", "Mango"]

Sorting Numbers

For numbers, use a compare function.

let arr = [30, 10, 20];

arr.sort(function(a, b) {
return a - b;
});

console.log(arr);

Output:

[10, 20, 30]

For descending order:

arr.sort(function(a, b) {
return b - a;
});
sort() modifies the original array.

17. reverse()

The reverse() method reverses the order of elements in an array.

Syntax

arr.reverse();

Example

let arr = [10, 20, 30];

arr.reverse();

console.log(arr);

Output:

[30, 20, 10]
reverse() modifies the original array.

18. join()

The join() method converts the elements of an array into a string.

Syntax

arr.join(separator);

Example

let arr = ["HTML", "CSS", "JavaScript"];

let result = arr.join(", ");

console.log(result);

Output:

HTML, CSS, JavaScript

You can use any separator:

arr.join("-");

Output:

HTML-CSS-JavaScript

Without specifying a separator:

arr.join();

The default separator is a comma.

19. reduce()

The reduce() method processes all elements of an array and reduces them to a single value.

Syntax

arr.reduce(function(accumulator, item, index, array) {
return newValue;
}, initialValue);

Example: Calculate Sum

let arr = [10, 20, 30, 40];

let total = arr.reduce(function(accumulator, item) {
return accumulator + item;
}, 0);

console.log(total);

Output:

100

The execution happens like this:

Initial value = 0

0 + 10 = 10
10 + 20 = 30
30 + 30 = 60
60 + 40 = 100

Therefore:

reduce() → many array elements → one final value

Arrow Function

The same code can be written as:

let total = arr.reduce((accumulator, item) => {
return accumulator + item;
}, 0);

Or in a shorter form:

let total = arr.reduce((acc, item) => acc + item, 0);