Array methods in JavaScript provide built-in functions to add, remove, update, search, and transform data stored in arrays. These methods make working with lists simple and efficient, and they are widely used in everyday JavaScript programming.


What Are Array Methods?

Array methods are functions available on JavaScript arrays that perform common operations such as:

  • Adding or removing elements

  • Finding items

  • Looping through data

  • Transforming arrays into new arrays


Adding and Removing Elements

push() and pop()

JavaScript
1let items = ["pen", "book"];
2items.push("eraser");
3items.pop();

Explanation:

  • push() adds an element to the end of the array

  • pop() removes the last element


shift() and unshift()

JavaScript
1let items = ["pen", "book"];
2items.unshift("marker");
3items.shift();

Explanation:

  • unshift() adds an element to the beginning

  • shift() removes the first element


Finding and Checking Elements

includes()

JavaScript
1let colors = ["red", "blue", "green"];
2console.log(colors.includes("blue"));

Explanation:
Checks whether the array contains the given value.


indexOf()

JavaScript
1let colors = ["red", "blue", "green"];
2console.log(colors.indexOf("green"));

Explanation:
Returns the index of the element or -1 if not found.


Looping and Transforming Arrays

forEach()

JavaScript
1let numbers = [1, 2, 3];
2numbers.forEach(function (num) {
3 console.log(num * 2);
4});

Explanation:
Runs a function for each element in the array.


map()

JavaScript
1let numbers = [1, 2, 3];
2let doubled = numbers.map(function (num) {
3 return num * 2;
4});
5console.log(doubled);

Explanation:
Creates a new array by transforming each element.


filter()

JavaScript
1let numbers = [10, 20, 30, 40];
2let result = numbers.filter(function (num) {
3 return num > 20;
4});
5console.log(result);

Explanation:
Creates a new array with elements that pass a condition.


Extracting and Combining Arrays

slice()

JavaScript
1let letters = ["a", "b", "c", "d"];
2console.log(letters.slice(1, 3));

Explanation:
Returns a portion of the array without changing the original array.


concat()

JavaScript
1let a = [1, 2];
2let b = [3, 4];
3console.log(a.concat(b));

Explanation:
Combines two arrays into a new array.


Common Mistakes with Array Methods

  • Forgetting that some methods return a new array (map, filter, slice)

  • Assuming all methods change the original array

  • Using map() when forEach() is enough

  • Not checking return values


Summary of Common Array Methods

MethodPurpose
push()Add to end
pop()Remove from end
shift()Remove from start
unshift()Add to start
includes()Check value
indexOf()Find index
forEach()Loop through
map()Transform
filter()Filter values
slice()Extract part
concat()Merge arrays

Conclusion

Array methods in JavaScript simplify working with collections of data. By using built-in methods like push, pop, map, filter, and slice, common tasks such as updating, searching, and transforming arrays become easy and readable in JavaScript applications.