January 28, 2025

Array Methods in JavaScript

Published by
Aida
4 published texts

[Array Methods in JavaScript]

---

# `push()`

- Adds one or more elements to the end of an array and returns the new length.

   let arr = [1, 2, 3];

  arr.push(4); // [1, 2, 3, 4]

  

---

# `pop()`

- Removes the last element from an array and returns it.

   let arr = [1, 2, 3];

  arr.pop(); // [1, 2]

  

---

# `shift()`

- Removes the first element from an array and returns it.

   let arr = [1, 2, 3];

  arr.shift(); // [2, 3]

  

---

# `unshift()`

- Adds one or more elements to the beginning of an array and returns the new length.

   let arr = [1, 2, 3];

  arr.unshift(0); // [0, 1, 2, 3]

  

---

# `concat()`

- Merges two or more arrays and returns a new array.

   let arr1 = [1, 2];

  let arr2 = [3, 4];

  let newArr = arr1.concat(arr2); // [1, 2, 3, 4]

  

---

# `slice()`

- Returns a shallow copy of a portion of an array as a new array (without modifying the original array).

   let arr = [1, 2, 3, 4];

  let newArr = arr.slice(1, 3); // [2, 3]

  

---

# `splice()`

- Adds or removes elements from an array and modifies the array.

   let arr = [1, 2, 3, 4];

  arr.splice(1, 2, 5); // [1, 5, 4]

  

---

# `map()`

- Executes a function on each element of an array and returns a new array.

   let arr = [1, 2, 3];

  let newArr = arr.map(x => x * 2); // [2, 4, 6]

  

---

# `filter()`

- Filters elements of an array based on a condition and returns a new array.

   let arr = [1, 2, 3, 4];

  let newArr = arr.filter(x => x > 2); // [3, 4]

  

---

# `reduce()`

- Reduces the elements of an array to a single value.

   let arr = [1, 2, 3, 4];

  let sum = arr.reduce((acc, val) => acc + val, 0); // 10

  

---

# `forEach()`

- Executes a function for each element of an array (does not return a value).

   let arr = [1, 2, 3];

  arr.forEach(x => console.log(x)); // 1, 2, 3

  

---

# `find()`

- Returns the first element that satisfies the given condition.

   let arr = [1, 2, 3, 4];

  let result = arr.find(x => x > 2); // 3

  

---

# `findIndex()`

- Returns the index of the first element that satisfies the given condition.

   let arr = [1, 2, 3, 4];

  let index = arr.findIndex(x => x > 2); // 2

  

---

# `some()`

- Checks if at least one element satisfies the given condition.

   let arr = [1, 2, 3, 4];

  let result = arr.some(x => x > 3); // true

  

---

# `every()`

- Checks if all elements satisfy the given condition.

   let arr = [1, 2, 3, 4];

  let result = arr.every(x => x > 0); // true

  

---

# `sort()`

- Sorts the elements of an array (by default as strings).

   let arr = [3, 1, 4, 2];

  arr.sort(); // [1, 2, 3, 4]

  

---

# `reverse()`

- Reverses the order of elements in an array.

   let arr = [1, 2, 3, 4];

  arr.reverse(); // [4, 3, 2, 1]

  

---

# `includes()`

- Checks if an array includes a certain value.

   let arr = [1, 2, 3, 4];

  let result = arr.includes(3); // true

  

---

# `join()`

- Converts all elements of an array into a string.

   let arr = [1, 2, 3, 4];

  let str = arr.join('-'); // "1-2-3-4"

  

---

#  `flat()`

- Flattens a multi-dimensional array into a single-dimensional array.

   let arr = [1, [2, [3, [4]]]];

  let flatArr = arr.flat(2); // [1, 2, 3, [4]]

  

---

These methods help you easily work with arrays in JavaScript.

Share this text