DEV Community

Guru prasanna
Guru prasanna

Posted on

Javascript - Array methods

Array Methods:

1. Adding/Removing Elements

  • push(): Adds an element to the end of the array.
let fruits = ["apple", "banana"];
fruits.push("cherry");
console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output: ["apple", "banana", "cherry"]

  • pop(): Removes the last element from the array.
fruits.pop();
console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output: ["apple", "banana"]

  • unshift(): Adds an element to the beginning of the array.
fruits.unshift("mango");
console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output: ["mango", "apple", "banana"]

  • shift(): Removes the first element from the array.
fruits.shift();
console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output: ["apple", "banana"]

2. Finding/Searching

  • indexOf(): Returns the index of the first occurrence of a value.
let numbers = [10, 20, 30, 40];
console.log(numbers.indexOf(20));
Enter fullscreen mode Exit fullscreen mode

Output: 1

  • includes(): Checks if the array contains a specific value.
console.log(numbers.includes(30)); 
console.log(numbers.includes(50));
Enter fullscreen mode Exit fullscreen mode

Output:

true
false
Enter fullscreen mode Exit fullscreen mode

3. Transforming Arrays

  • map(): Creates a new array by transforming each element.
let numbers = [1, 2, 3];
let squares = numbers.map(num => num * num);
console.log(squares);
Enter fullscreen mode Exit fullscreen mode

Output: [1, 4, 9]

  • filter(): Creates a new array with elements that pass a condition.
let evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers);
Enter fullscreen mode Exit fullscreen mode

Output: [2]

  • reduce(): Reduces the array to a single value.
let sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum);
Enter fullscreen mode Exit fullscreen mode

Output: 6

4. Sorting

  • sort(): Sorts the array (alphabetically by default).
let fruits = ["banana", "apple", "cherry"];
fruits.sort();
console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output: ["apple", "banana", "cherry"]

For numbers, you need a custom comparator:

let numbers = [10, 5, 20];
numbers.sort((a, b) => a - b); // Ascending
console.log(numbers);
Enter fullscreen mode Exit fullscreen mode

Output: [5, 10, 20]

  • reverse(): Reverses the array.
 fruits.reverse();
 console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output: ["cherry", "banana", "apple"]

5. Combining/Extracting

  • concat(): Combines two or more arrays.
let arr1 = [1, 2];
let arr2 = [3, 4];
let combined = arr1.concat(arr2);
console.log(combined);
Enter fullscreen mode Exit fullscreen mode

Output: [1, 2, 3, 4]

  • slice(): Extracts a portion of an array without modifying it.
let numbers = [10, 20, 30, 40];
let sliced = numbers.slice(1, 3);
console.log(sliced);
Enter fullscreen mode Exit fullscreen mode

Output: [20, 30]

  • splice(): Adds or removes elements from an array.
let fruits = ["apple", "banana", "cherry"];
fruits.splice(1, 1, "orange", "grape");
console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output: ["apple", "orange", "grape", "cherry"]

6. Iterating Over an Array

  • forEach(): Executes a function for each array element.
let numbers = [1, 2, 3];
numbers.forEach(num => console.log(num * 2));
Enter fullscreen mode Exit fullscreen mode

Output: 2, 4, 6

7. Other Useful Methods

  • join(): Converts an array into a string, with a specified delimiter.

let fruits = ["apple", "banana", "cherry"];
console.log(fruits.join(", "));
Enter fullscreen mode Exit fullscreen mode

Output: "apple, banana, cherry"

  • find(): Returns the first element that satisfies a condition.

let numbers = [10, 20, 30];
let result = numbers.find(num => num > 15);
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output: 20

  • findIndex(): Returns the index of the first element that satisfies a condition.

console.log(numbers.findIndex(num => num > 15));
Enter fullscreen mode Exit fullscreen mode

Output: 1

  • flat(): Flattens a nested array.
let nested = [1, [2, 3], [4, [5]]];
console.log(nested.flat(2));
Enter fullscreen mode Exit fullscreen mode

Output: [1, 2, 3, 4, 5]

Top comments (0)