Code Example Arrow Function Syntax - markpawl/angular-learning-portal GitHub Wiki

Arrow Function Syntax Code Examples 2/23/2020

/* arrow_functions.js / / Examples of Arrow Function Syntax */

// regular functions function add(a,b){ return a + b; } function squared(a){ return a * a; }

// arrow function taking one parameter // returns the result of the statement on the right var arrow_squared = a => a * a

// arrow function using angle brackets on right hand side // see that the 'return' statement is now explicitly stated var arrow_squared_2 = a =>{ return a * a }

// arrow function taking multiple parameters // note that the parameter list on the left now has parenthesis around it var arrow_add = (a,b) => a + b

// arrow function with multiple lines of code var arrow_add_2 = (a,b) =>{ let x = a; let y = b; let z = x + y; return z; }

// examples using the arrow functions console.log( 'add(1,2): ' + add(1,2)); console.log( 'arrow_add(3,4): ' + arrow_add(3,4)); console.log( 'arrow_add_2(5,6): ' + arrow_add_2(5,6)); console.log( 'squared(3): ' + squared(3) ); console.log( 'arrow_squared(4): ' + arrow_squared(4)); console.log( 'arrow_squared_2(5): ' + arrow_squared_2(5));

/* Output: D:\LabWorkJS>node arrow_functions.js add(1,2): 3 arrow_add(3,4): 7 arrow_add_2(5,6): 11 squared(3): 9 arrow_squared(4): 16 arrow_squared_2(5): 25 */