ES6: 6 Use Arrow Functions to Write Concise Anonymous Functions
In JavaScript, we often don't need to name our
functions, especially when passing a function as an argument to another
function. Instead, we create inline functions. We don't need to name
these functions because we do not reuse them anywhere else.
To achieve this, we often use the following syntax:
const myFunc = function() {
const myVar = "value";
return myVar;
}
const myVar = "value";
return myVar;
}
ES6 provides us with the syntactic sugar to not have to write anonymous functions this way. Instead, you can use arrow function syntax:
const myFunc = () => {
const myVar = "value";
return myVar;
}
When there is no function body, and only a return value, arrow function syntax allows you to omit the keyword return as well as the brackets surrounding the code. This helps simplify smaller functions into one-line statements:
const myVar = "value";
return myVar;
}
const myFunc= () => "value"
This code will still return value by default.
ES6: 7 Write Arrow Functions with Parameters
just like a normal function, you can pass arguments into arrow functions.
// doubles input value and returns it
const doubler = (item) => item * 2;
You can pass more than one argument into arrow functions as well.
more than one argument
const doubler = (item) => item * 2;
const myConcat = (arr1, arr2)=> {
"use strict";
return arr1.concat(arr2);
};
// test your code
console.log(myConcat([1, 2], [3, 4, 5]));
"use strict";
return arr1.concat(arr2);
};
// test your code
console.log(myConcat([1, 2], [3, 4, 5]));
ES6: 8 Write Higher Order Arrow Functions
Arrow functions work really well with higher order functions, such as map(), filter(), and reduce(), that take other functions as arguments for processing collections of data.
Read the following code:
FBPosts.filter(function(post) {
return post.thumbnail !== null && post.shares > 100 && post.likes > 500;
})
We have written this with filter() to at least make it somewhat readable. Now compare it to the following code which uses arrow function syntax instead:
return post.thumbnail !== null && post.shares > 100 && post.likes > 500;
})
FBPosts.filter((post) => post.thumbnail !== null && post.shares > 100 && post.likes > 500)
This code is more succinct and accomplishes the same task with fewer lines of code.
lets use arrow function syntax to compute the square of only the positive integers (fractions are not integers) in the array realNumberArray and store the new array in the variable squaredIntegers.
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
"use strict";
const squaredIntegers=arr.map(x=> x>0 && x%1==0 && x*x).filter(Number);
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
const squareList = (arr) => {
"use strict";
const squaredIntegers=arr.map(x=> x>0 && x%1==0 && x*x).filter(Number);
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
Comments
Post a Comment