The Spread Operator expands an array into its elements. It can also be used for object literals.
Why should I use it?
Example:
Let’s say you want to show a list of favorite foods without creating a loop function. Use a spread operator like this:
The for...of
statement loops/iterates through the collection, and provides you the ability to modify specific items. It replaces the conventional way of doing a
for-loop
.
Why should I use it?
Example:
Let’s say you have a toolbox, and you want to show all the tools inside it. The
for...of
iterator makes it easy.
The
includes()
method is used to check if a specific string exists in a collection, and returns
true
or
false
. Keep in mind that it is case sensitive: if the item inside the collection is
SCHOOL
, and you search for
school
, it will return
false
.
Returns a Boolean value that indicates whether the passed in string is contained in the string object.
Why should I use it?
Example:
Let’s say for whatever reason that you are not aware of what cars you have in your garage, and you need a system to check if the car you want exists or not.
includes()
to the rescue!
The
some()
method checks if some elements exists in an array, and returns
true
or
false
. This is somewhat similar to the concept of the
includes()
method, except the argument is a function and not a string.
Why should I use it?
Example:
Let’s say you are a club owner, and don’t care who enters the club. But some are not allowed in, because they have been drinking too much (my creativity at its best). Check out the differences between ES5 and ES6 below:
The
every()
method loops through the array, checks every
item, and returns
true
or
false
. Same concept as
some()
. Except every item must satisfy the conditional statement, otherwise, it will return
false
.
Why should I use it?
Example:
The last time you allowed
some()
underage students to enter the club, someone reported this and the police caught you. This time you won’t let that happen, and you’ll make sure that
everyone passes the age limit with the
/ Define the callback function.
function CheckIfEven(value, index, ar) {
document.write(value + " ");
if (value % 2 == 0)
return true;
else
return false;
}
// Create an array.
var numbers = [2, 4, 5, 6, 8];
// Check whether the callback function returns true for all of the
// array values.
if (numbers.every(CheckIfEven))
document.write("All are even.");
else
document.write("Some are not even.");
// Output:
// 2 4 5 Some are not even.
every()
operator.
The
filter()
method creates a new array with all elements that pass the test.
Why should I use it?
Example:
Let’s say you want to return only prices that are above or equal to 30. Filter out all those other prices…
The
map()
method is similar to the
filter()
method in terms of returning a new array. However, the only difference is that it is used to modify items.
Why should I use it?
Example:
Let’s say you have a list of products with prices. Your manager needs a list to show the new prices after they have been taxed by 25%. The
map()
method can help you out.
The
reduce()
method can be used to transform an array into something else, which could be an integer, an object, a chain of promises ( sequential execution of promises) etc. For practical reasons, a simple use case would be to sum a list of integers. In short, it “reduces” the whole array into one value.
Why should I use it?
Example:
Let’s say you want to find out your total expenses for a week. Use
reduce()
to get that value.