Arrow functions have a particular advantage
as callbacks. Let's look at an example of
how much the code is simplified in this
case. For example, let's say we have the following
filter
function with callback:
let result = filter([1, 2, 3, 4, 5], function(elem) {
if (elem % 2 === 0) {
return true;
} else {
return false;
}
});
First, let's get rid of the construction
if
and write the condition simply
through the operator ===
:
let result = filter([1, 2, 3, 4, 5], function(elem) {
return elem % 2 == 0;
});
Let us now replace the ordinary function with an arrow one:
let result = filter([1, 2, 3, 4, 5], elem => elem % 2 == 0);
Given the following function with a callback:
let result = every([1, 2, 3, 4, 5], function(elem) {
if (elem > 0) {
return true;
} else {
return false;
}
});
Simplify the callback with an arrow function.
Given the following function with a callback:
let result = every([1, 2, 3, 4, 5], function(elem, index) {
if (elem * index > 10) {
return true;
} else {
return false;
}
});
Simplify the callback with an arrow function.
Given the following function with a callback:
let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
let result = each(arr, function(elem, index) {
if (elem * index > 10) {
return true;
} else {
return false;
}
});
Simplify the callback with an arrow function.