6 New Array Functions in ES6

6 New Array Functions in ES6

JavaScript built-in array methods

ยท

3 min read

Play this article

ES6, also known as ECMAScript 2015, brought several new features to JavaScript, including new array functions. In this blog, we'll take a look at six of these new functions and how they can make your life easier when working with arrays in JavaScript.

USING MAP()

Definition and Usage

The map() the method creates a new array with the results of calling a function for every array element.

The map() method calls the provided function once for each element in an array, in order.

Note: map() does not execute the function for array elements without values.

Note: this method does not change the original array.

JavaScript map vs for loop

USING FOREACH()

Definition and Usage

The forEach() method calls a function once for each element in an array, in order.

Note: the function is not executed for array elements without values.

JavaScript forEach vs for loop

USING FILTER()

Definition and Usage

The filter()the method creates an array filled with all array elements that pass a test (provided as a function).

Note: filter() does not execute the function for array elements without values.

Note: filter() does not change the original array.

JavaScript filter vs for loop

USING REDUCE()

Definition and Usage

The reduce() the method reduces the array to a single value.

The reduce() the method executes a provided function for each value of the array (from left to right).

The return value of the function is stored in an accumulator (result/total).

Note: reduce() does not execute the function for array elements without values.

Note: this method does not change the original array.

JavaScript reduce vs for loop

USING SORT()

Definition and Usage

The sort() method sorts the items of an array.

The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down).

By default, the sort() method sorts the values as strings in alphabetical and ascending order.

Note: This method changes the original array.

JavaScript sort vs for loop

USING FINDINDEX()

Definition and Usage

The findIndex() the method returns the index of the first element in an array that passes a test (provided as a function).

The findIndex() the method executes the function once for each element present in the array:

  • If it finds an array element where the function returns a true value, findIndex() returns the index of that array element (and does not check the remaining values)

  • Otherwise, it returns -1

Note: findIndex() does not execute the function for array elements without values.

Note: findIndex() does not change the original array.

JavaScript findIndex vs for loop

In conclusion, these six new array functions in ES6 provide a powerful and convenient set of tools for working with arrays in JavaScript. They allow you to perform common array operations with greater ease and make your code more concise and readable. Whether you're just starting out with JavaScript or are an experienced developer, it's worth taking some time to learn these functions and incorporate them into your workflow.

Thank you for reading!

Did you find this article valuable?

Support Bhushan Patil by becoming a sponsor. Any amount is appreciated!

ย