Frequently used Array methods in Javascript

Frequently used Array methods in Javascript

Table of contents

No heading

No headings in the article.

Arrays are an essential part of the JavaScript programming language, and they allow developers to store multiple values in a single variable. JavaScript provides a wide range of array methods that allow developers to manipulate arrays in various ways, such as adding or removing elements, sorting and searching for elements, and much more. With so many methods available, it can be challenging to determine which method to use for a particular task. In this blog post, we will discuss the most commonly used array methods in JavaScript with code examples and explanations, which will help developers become proficient in working with arrays and make the most of their functionality. Whether you're a beginner or an experienced developer, this blog post will provide you with a comprehensive overview of the array methods in JavaScript, enabling you to write more efficient and effective code.

1. concat(): This method joins two or more arrays and returns a new array.

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const newArray = array1.concat(array2);
console.log(newArray); // [1, 2, 3, 4, 5, 6]
  1. every(): This method checks if every element in the array passes a certain condition, and returns a boolean value.
const array = [1, 2, 3, 4, 5];
const isGreaterThanZero = (value) => value > 0;
console.log(array.every(isGreaterThanZero)); // true
  1. filter(): This method creates a new array with all the elements that pass a certain condition.
const array = [1, 2, 3, 4, 5];
const isEven = (value) => value % 2 === 0;
const newArray = array.filter(isEven);
console.log(newArray); // [2, 4]
  1. find(): This method returns the first element in the array that passes a certain condition, or undefined if no element passes the condition.
const array = [1, 2, 3, 4, 5];
const isGreaterThanThree = (value) => value > 3;
console.log(array.find(isGreaterThanThree)); // 4
  1. findIndex(): This method returns the index of the first element in the array that passes a certain condition, or -1 if no element passes the condition.
const array = [1, 2, 3, 4, 5];
const isGreaterThanThree = (value) => value > 3;
console.log(array.findIndex(isGreaterThanThree)); // 3
  1. forEach(): This method calls a function for each element in the array.
const array = [1, 2, 3, 4, 5];
const printValue = (value) => console.log(value);
array.forEach(printValue); // prints 1, 2, 3, 4, 5
  1. includes(): This method checks if the array includes a certain element, and returns a boolean value.
const array = [1, 2, 3, 4, 5];
console.log(array.includes(3)); // true
  1. indexOf(): This method returns the index of the first occurrence of a certain element in the array, or -1 if the element is not found.
const array = [1, 2, 3, 4, 5];
console.log(array.indexOf(3)); // 2
  1. join(): This method joins all the elements in the array into a string, using a separator if specified.
const array = [1, 2, 3, 4, 5];
console.log(array.join()); // "1,2,3,4,5"
console.log(array.join("-")); // "1-2-3-4-5"
  1. map(): This method creates a new array with the results of calling a function for each element in the array.
const array = [1, 2, 3, 4, 5];
const doubleValue = (value) => value * 2;
const newArray = array.map(doubleValue);
console.log(newArray); // [2,
  1. pop(): This method removes the last element from the array and returns it.
const array = [1, 2, 3, 4, 5];
const lastElement = array.pop();
console.log(lastElement); // 5
console.log(array); // [1, 2, 3, 4]
  1. push(): This method adds one or more elements to the end of the array and returns the new length of the array.
const array = [1, 2, 3];
const newLength = array.push(4, 5);
console.log(newLength); // 5
console.log(array); // [1, 2, 3, 4, 5]
  1. reduce(): This method applies a function to each element in the array, and reduces the array to a single value.
const array = [1, 2, 3, 4, 5];
const sum = (accumulator, currentValue) => accumulator + currentValue;
const total = array.reduce(sum);
console.log(total); // 15
  1. reverse(): This method reverses the order of the elements in the array.
const array = [1, 2, 3, 4, 5];
array.reverse();
console.log(array); // [5, 4, 3, 2, 1]
  1. shift(): This method removes the first element from the array and returns it.
const array = [1, 2, 3, 4, 5];
const firstElement = array.shift();
console.log(firstElement); // 1
console.log(array); // [2, 3, 4, 5]
  1. slice(): This method creates a new array that contains a portion of the original array.
const array = [1, 2, 3, 4, 5];
const newArray = array.slice(1, 3);
console.log(newArray); // [2, 3]
  1. some(): This method checks if at least one element in the array passes a certain condition, and returns a boolean value.
const array = [1, 2, 3, 4, 5];
const isGreaterThanFour = (value) => value > 4;
console.log(array.some(isGreaterThanFour)); // true
  1. sort(): This method sorts the elements in the array.
const array = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
array.sort();
console.log(array); // [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
  1. splice(): This method adds or removes elements from the array.
const array = [1, 2, 3, 4, 5];
array.splice(2, 1, 6);
console.log(array); // [1, 2, 6, 4, 5]
  1. toString(): This method converts the array to a string.
const array = [1, 2, 3, 4, 5];
console.log(array.toString()); // "1,2,3,4,5"
  1. unshift(): This method adds one or more elements to the beginning of the array and returns the new length of the array.
const array = [1, 2, 3];
const newLength = array.unshift(0, -1);
console.log(newLength); // 5
console.log(array); // [-1, 0, 1, 2, 3]
  1. valueOf(): This method returns the primitive value of the array.
const array = [1, 2, 3, 4, 5];
console.log(array.valueOf()); // [1, 2, 3, 4, 5]
  1. includes(): This method checks if an element is included in the array and returns a boolean value.
const array = [1, 2, 3, 4, 5];
console.log(array.includes(3)); // true
console.log(array.includes(6)); // false
  1. find(): This method returns the value of the first element in the array that satisfies a certain condition, or undefined if no such element is found.
const array = [1, 2, 3, 4, 5];
const isGreaterThanThree = (value) => value > 3;
console.log(array.find(isGreaterThanThree)); // 4
  1. findIndex(): This method returns the index of the first element in the array that satisfies a certain condition, or -1 if no such element is found.
const array = [1, 2, 3, 4, 5];
const isGreaterThanThree = (value) => value > 3;
console.log(array.findIndex(isGreaterThanThree)); // 3
  1. flat(): This method creates a new array that is flattened to a certain depth.
const array = [1, [2, [3, [4, [5]]]]];
console.log(array.flat()); // [1, 2, [3, [4, [5]]]]
console.log(array.flat(2)); // [1, 2, 3, [4, [5]]]
  1. flatMap(): This method creates a new array by applying a function to each element in the array, and then flattening the result to a certain depth.
const array = [1, 2, 3];
const double = (value) => [value * 2];
console.log(array.flatMap(double)); // [2, 4, 6]
  1. from(): This method creates a new array from an array-like or iterable object.
const arrayLikeObject = {0: 'a', 1: 'b', 2: 'c', length: 3};
console.log(Array.from(arrayLikeObject)); // ['a', 'b', 'c']

const set = new Set([1, 2, 3]);
console.log(Array.from(set)); // [1, 2, 3]
  1. of(): This method creates a new array from a set of values.
console.log(Array.of(1, 2, 3)); // [1, 2, 3]
  1. keys(): The keys() method in JavaScript returns a new array iterator object that contains the keys (indexes) for each index in the array.

Here's an example of how to use the keys() method:

const array = ['apple', 'banana', 'orange'];

const iterator = array.keys();

for (const key of iterator) {
  console.log(key);
}

In this example, we create an array called array containing three string values. We then call the keys() method on the array, which returns a new array iterator object.

We then use a for...of loop to iterate over the keys in the iterator. On each iteration, the loop variable key represents a key (index) from the array. We log the key to the console.

The output of this code will be:

Copy code0
1
2

This demonstrates how the keys() method can be used to access the keys (indexes) of an array in a convenient way.

  1. values(): The values() method in JavaScript returns a new array iterator object that contains the values for each index in the array.

Here's an example of how to use the values() method:

const array = ['apple', 'banana', 'orange'];

const iterator = array.values();

for (const value of iterator) {
  console.log(value);
}

In this example, we create an array called array containing three string values. We then call the values() method on the array, which returns a new array iterator object.

We then use a for...of loop to iterate over the values in the iterator. On each iteration, the loop variable value represents a value from the array. We log the value to the console.

The output of this code will be:

'apple'
'banana'
'orange'

This demonstrates how the values() method can be used to access the values of an array in a convenient way. It's worth noting that the values() method is the default iterator for arrays, so you can also use a simple for...of loop to iterate over the values of an array without explicitly calling the values() method.

  1. entries():

The entries() method in JavaScript returns a new array iterator object that contains the key-value pairs for each index in the array. The key-value pairs are returned as arrays, with the first element representing the index and the second element representing the value.

Here's an example of how to use the entries() method:

const array = ['apple', 'banana', 'orange'];

const iterator = array.entries();

for (const entry of iterator) {
  console.log(entry);
}

In this example, we create an array called array containing three string values. We then call the entries() method on the array, which returns a new array iterator object.

We then use a for...of loop to iterate over the entries in the iterator. On each iteration, the loop variable entry represents a key-value pair, which is an array containing the index and the corresponding value. We log the entry value to the console.

  1. reduce(): It is a powerful method in JavaScript that allows you to iterate over an array and reduce it to a single value. It takes two arguments: a callback function and an optional initial value.

The callback function takes two parameters: an accumulator and the current element being processed. The accumulator is the value returned from the previous iteration of the function, and the current element is the current element being processed. The callback function should return a new accumulator value for the next iteration.

Here's an example that uses reduce() to sum up all the elements in an array:

const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => {
  return accumulator + currentValue;
}, 0);
console.log(sum); // Output: 15

In this example, we start with an initial value of 0 for the accumulator. On the first iteration, the callback function takes the value of 0 as the accumulator and 1 as the current value, and returns the sum of 0 and 1, which is 1. On the second iteration, the callback function takes 1 as the accumulator and 2 as the current value and returns 3. This continues until all the elements in the array have been processed, and the final value of the accumulator is returned as the result of the reduce() method.

You can use reduce() for other tasks, such as finding the maximum value in an array or flattening an array of arrays into a single array. It's a versatile method that can be used in many different scenarios, so it's worth learning how to use it effectively.