Most Common Array Methods in ES6 – JavaScript


ES6, also known as ECMAScript 2015 (ES2015), is the sixth edition of the ECMAScript standard. ECMAScript is the standard upon which JavaScript is based, and it is used to define the features of the JavaScript programming language.

ES6 introduced significant enhancements and new features to JavaScript, making the language more powerful and expressive.

In this article, we will see some common ES6 methods with examples.

Includes() method

The JavaScript includes() method was introduced with ES6, and it is the most common and modern way of checking element inside an array or string.

Let’s take an example of finding an array element in the array.

// Checking array element exists in the array
const arr = [1, 4, 5, 6];
let exists = arr.includes(3);
console.log(exists); // false
exists = arr.includes(4);
console.log(exists); // true

set() Object

A set is a data structure that allows storage of unique values of any type, whether primitive values or object references.

In this example, we will use the set object in javascript to remove duplicate elements from an array.

// Remove duplicates from array using set object
const arr = [2, 3, 3, 4, 5, 5, 6];
const uniqueArray = [ Set(arr)];
console.log(uniqueArray); // Output: [2, 3, 4, 5, 6]

indexOf() Method

The indexOf() method in JavaScript is a built-in method for strings and arrays that allows you to determine the first occurrence of a specified value within the string or array.

In this example, we will find the index of an element in an array using the indexOf() method.

// Find index of an element in array using indexOf()
const array = [10, 20, 30, 40, 50];
const element = 30;
const index = array.indexOf(element);
console.log(index); // Output: 2

splice() Method

In JavaScript, the splice() method is a powerful array method that allows you to modify the contents of an array by removing, replacing, or adding elements.

In this example, we will see how we can remove an element from an array by its index using the splice() method.

// Remove array element by its index using splice()
const array = [10, 20, 30, 40, 50];
const index = 2;
array.splice(index, 1);
console.log(array); // Output: [10, 20, 40, 50]

Spread Operator

The spread operator, denoted by three dots (...) is used to expand or spread an iterable or an array.

In this example, we will use the spread operator to concatenate two arrays.

// Concatenating two arrays using spread operator
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const concatenatedArray = [...array1, ...array2];
console.log(concatenatedArray); // Output: [1, 2, 3, 4, 5, 6]

Sort() Method

In JavaScript, the sort() method is used to sort the elements of an array in place and returns the sorted array. By default, the sort() method converts elements of the array into strings and sorts them in lexicographic (dictionary) order.

In this example, we will use the sort() method to sort the given array in ascending order.

// Sorting array in ascending order using sort()
const array = [5, 3, 1, 4, 2];
array.sort((a, b) => a - b);
console.log(array); // Output: [1, 2, 3, 4, 5]

reduce() Method

In JavaScript, the reduce() method is used to reduce an array to a single value. It executes a provided function for each array element, resulting in a single output value.

In this example, we are going to use the reduce() method to find the sum of the given array.

// Using reduce to find the sum of array elements
const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // Output: 15