Lesson 17 of 30 – JavaScript Array Methods
50%

JavaScript Array Methods

JavaScript provides many built-in array methods to add, remove, search, sort, and manipulate array elements efficiently.

Note: Array methods make programming easier and reduce the amount of code.
What are Array Methods?

Array methods are predefined functions that perform operations on arrays.

They help you:

  • Add elements.
  • Remove elements.
  • Search elements.
  • Sort arrays.
  • Create new arrays.
  • Loop through arrays.
Common Array Methods
Method Purpose
push() Add element at end.
pop() Remove last element.
shift() Remove first element.
unshift() Add element at beginning.
concat() Combine arrays.
slice() Extract array portion.
splice() Add or remove elements.
join() Convert array to string.
push() Method

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

Example:
let fruits=["Apple","Mango"];

fruits.push("Orange");

console.log(fruits);

Output:

Apple, Mango, Orange

pop() Method

Removes the last element from an array.

Example:
let fruits=["Apple","Mango","Orange"];

fruits.pop();

Output:

Apple, Mango

shift() and unshift()

shift() removes the first element.

unshift() adds elements at the beginning.

Example:
let city=["Delhi","Patna"];

city.unshift("Kolkata");

city.shift();
concat() Method

Combines two or more arrays.

Example:
let a=[1,2];
let b=[3,4];

let c=a.concat(b);

Output:

1,2,3,4

slice() Method

The slice() method returns selected elements as a new array.

It does not change the original array.

Example:
let fruits=["Apple","Mango","Orange","Banana"];

let x=fruits.slice(1,3);

console.log(x);

Output:

Mango, Orange

splice() Method

The splice() method adds or removes elements from an array.

Example:
let fruits=["Apple","Mango","Orange"];

fruits.splice(1,1,"Banana");

console.log(fruits);

Output:

Apple, Banana, Orange

join() Method

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

Example:
let fruits=["Apple","Mango","Orange"];

console.log(fruits.join("-"));

Output:

Apple-Mango-Orange

indexOf() Method

Returns the first index of an element.

Example:
let fruits=["Apple","Mango","Orange"];

console.log(fruits.indexOf("Mango"));

Output:

1

lastIndexOf() Method

Returns the last occurrence of an element.

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

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

Output:

3

includes() Method

Checks whether an element exists in an array.

Example:
let fruits=["Apple","Mango","Orange"];

console.log(fruits.includes("Mango"));

Output:

true

forEach() Method

Executes a function for every array element.

Example:
let num=[10,20,30];

num.forEach(function(i){
console.log(i);
});

Output:

10
20
30

map() Method

Creates a new array by performing an operation on each element.

Example:
let num=[1,2,3];

let result=num.map(function(i){
return i*2;
});

console.log(result);

Output:

2,4,6

filter() Method

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

Example:
let num=[10,20,30,40];

let result=num.filter(function(x){
return x>20;
});

console.log(result);

Output: 30, 40

find() and findIndex()

find() returns the first matching value.
findIndex() returns the index of the first matching value.

Example:
let num=[10,20,30,40];

console.log(num.find(x=>x>25));
console.log(num.findIndex(x=>x>25));

Output:
30
2

some() and every()

some() checks whether at least one element satisfies a condition.
every() checks whether all elements satisfy a condition.

Example:
let num=[10,20,30];

console.log(num.some(x=>x>25));
console.log(num.every(x=>x>5));

Output:
true
true

sort() and reverse()

sort() arranges elements.
reverse() reverses the order.

Example:
let arr=[5,2,8,1];

arr.sort();
arr.reverse();
reduce() Method

The reduce() method combines all array values into a single value.

Example:
let num=[10,20,30];

let total=num.reduce(function(sum,x){
return sum+x;
});

console.log(total);

Output: 60

Key Points
  • push() adds elements at the end.
  • pop() removes the last element.
  • shift() removes the first element.
  • unshift() adds elements at the beginning.
  • concat() joins arrays.
  • slice() extracts elements.
  • splice() adds or removes elements.
  • map() creates a new transformed array.
  • filter() selects matching elements.
  • reduce() returns a single value.
  • sort() sorts elements.
  • reverse() reverses the array.
Quick Revision
Method Purpose
push() Add Last
pop() Remove Last
shift() Remove First
unshift() Add First
map() Transform Data
filter() Filter Data
reduce() Single Value
sort() Sort Elements
Best Practices
  • Use push() instead of manual indexing.
  • Prefer map() and filter() for cleaner code.
  • Avoid modifying the original array unless necessary.
  • Use includes() for checking values.
  • Use reduce() for calculations.
  • Choose the correct method for better performance.

🧠 Quick Quiz

Which method adds an element to the end of an array?