arrow function es7 code example
Example 1: js arrow function
// const add = function(x,y) {
// return x + y;
// }
// const add = (x, y) => {
// return x + y;
// }
const add = (a, b) => a + b;
const square = num => {
return num * num;
}
// const rollDie = () => {
// return Math.floor(Math.random() * 6) + 1
// }
const rollDie = () => (
Math.floor(Math.random() * 6) + 1
)
Example 2: es6 arrow function
//ES5
var phraseSplitterEs5 = function phraseSplitter(phrase) {
return phrase.split(' ');
};
//ES6
const phraseSplitterEs6 = phrase => phrase.split(" ");
console.log(phraseSplitterEs6("ES6 Awesomeness")); // ["ES6", "Awesomeness"]