1//Long version
2function add(a, b) {
3 return a + b;
4}
5
6// Shorthand
7const add = (a, b) => a + b;
8
1// Arrow function with two arguments const sum = (firstParam, secondParam) => { return firstParam + secondParam; }; console.log(sum(2,5)); // Prints: 7 // Arrow function with no arguments const printHello = () => { console.log('hello'); }; printHello(); // Prints: hello // Arrow functions with a single argument const checkWeight = weight => { console.log(`Baggage weight : ${weight} kilograms.`); }; checkWeight(25); // Prints: Baggage weight : 25 kilograms. // Concise arrow functionsconst multiply = (a, b) => a * b; console.log(multiply(2, 30)); // Prints: 60
1// Traditional Function
2function (a){
3 return a + 100;
4}
5
6// Arrow Function Break Down
7
8// 1. Remove the word "function" and place arrow between the argument and opening body bracket
9(a) => {
10 return a + 100;
11}
12
13// 2. Remove the body brackets and word "return" -- the return is implied.
14(a) => a + 100;
15
16// 3. Remove the argument parentheses
17a => a + 100;