JavaScript Array Methods
8 April 2025 | Category: JavaScript
JavaScript arrays come with powerful built-in methods that make it easy to add, remove, find, sort, and manipulate array elements.
These methods make working with data much faster and easier.
✅ 1. push()
– Add to End
Adds an element to the end of an array.
let fruits = ["Apple", "Banana"];
fruits.push("Mango");
console.log(fruits); // ["Apple", "Banana", "Mango"]
✅ 2. pop()
– Remove from End
Removes the last element from an array.
fruits.pop();
console.log(fruits); // ["Apple", "Banana"]
✅ 3. shift()
– Remove from Start
Removes the first element from an array.
fruits.shift();
console.log(fruits); // ["Banana"]
✅ 4. unshift()
– Add to Start
Adds an element to the beginning of an array.
fruits.unshift("Orange");
console.log(fruits); // ["Orange", "Banana"]
✅ 5. concat()
– Merge Arrays
Combines two or more arrays into one new array.
let arr1 = [1, 2];
let arr2 = [3, 4];
let combined = arr1.concat(arr2);
console.log(combined); // [1, 2, 3, 4]
✅ 6. join()
– Convert Array to String
Returns a string by joining all elements with a separator.
let colors = ["Red", "Green", "Blue"];
console.log(colors.join(", ")); // "Red, Green, Blue"
✅ 7. slice()
– Extract a Portion
Returns a part of the array (without changing the original array).
let items = ["a", "b", "c", "d"];
let sliced = items.slice(1, 3);
console.log(sliced); // ["b", "c"]
✅ 8. splice()
– Add or Remove Items
Changes the contents of the array by removing/replacing elements.
let nums = [1, 2, 3, 4];
nums.splice(2, 1, 99); // From index 2, remove 1, add 99
console.log(nums); // [1, 2, 99, 4]
✅ 9. indexOf()
– Find Index
Finds the first index of a value.
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.indexOf("Banana")); // 1
✅ 10. includes()
– Check if Exists
Checks if an item is present in the array.
console.log(fruits.includes("Mango")); // true
✅ 11. reverse()
– Reverse Order
Reverses the order of elements in the array.
let arr = [1, 2, 3];
arr.reverse();
console.log(arr); // [3, 2, 1]
✅ 12. sort()
– Sort Alphabetically or Numerically
let names = ["John", "Adam", "Zara"];
names.sort();
console.log(names); // ["Adam", "John", "Zara"]
let numbers = [30, 4, 100];
numbers.sort((a, b) => a - b);
console.log(numbers); // [4, 30, 100]
✅ 13. forEach()
– Loop Over Elements
Runs a function for each item in the array.
let nums = [10, 20, 30];
nums.forEach(function(num) {
console.log(num);
});
✅ 14. map()
– Transform Each Item
Returns a new array after modifying each item.
let nums = [1, 2, 3];
let doubled = nums.map(num => num * 2);
console.log(doubled); // [2, 4, 6]
✅ 15. filter()
– Filter Items
Returns a new array with items that match a condition.
let nums = [5, 12, 8, 130, 44];
let result = nums.filter(num => num > 10);
console.log(result); // [12, 130, 44]
✅ 16. reduce()
– Reduce to Single Value
Applies a function to reduce the array to a single value.
let nums = [1, 2, 3, 4];
let sum = nums.reduce((total, current) => total + current);
console.log(sum); // 10
🧠 Summary Table
Method | Description |
---|---|
push() | Add to end |
pop() | Remove from end |
shift() | Remove from start |
unshift() | Add to start |
concat() | Merge arrays |
join() | Join elements to string |
slice() | Extract part of array |
splice() | Add/Remove items |
indexOf() | Find index of item |
includes() | Check if item exists |
reverse() | Reverse array |
sort() | Sort array |
forEach() | Loop through elements |
map() | Modify and return new array |
filter() | Filter items based on condition |
reduce() | Reduce array to a single value |