arrow functions nodejs code example
Example 1: javascript arrow function
let add = function(x,y) {
return x + y;
}
console.log(add(10,20));
let add = (x,y) => x + y;
console.log(add(10,20));
let add = (x, y) => { return x + y; };
Example 2: js arrow function
const add = (a, b) => a + b;
const square = num => {
return num * num;
}
const rollDie = () => (
Math.floor(Math.random() * 6) + 1
)
Example 3: how to make javascript function consise
multiplyfunc = (a, b) => { return a * b; }
Example 4: arrow function javascript
let testingFunc(string) => string == "Test" ? "Success!" : "Failure!";
console.log(testingFunc("test"));
let arrowFunc(string) => {
if (string = "test") {
return "Success!";
}
return "Failure!";
}
};
console.log(testingFunc("Test"));
Example 5: es6 arrow function
var phraseSplitterEs5 = function phraseSplitter(phrase) {
return phrase.split(' ');
};
const phraseSplitterEs6 = phrase => phrase.split(" ");
console.log(phraseSplitterEs6("ES6 Awesomeness"));