Tips and Gotchas for ES6 Arrow Functions

Video

JavaScript Notes

JavaScript
    // Tips and Gotchas for Arrow Functions
    
    let retVal = 42;
    let obj = {'prop1': 'foo', 'prop2': 'bar'};
    
    //1. One parameter with and without curly braces
    const f1 = (param) => {return param};	// must add return when using curly braces 
    console.log( f1(7) ) ;
    const f2 = param => param; 
    console.log( f2(7) );
    
    //2. No parameters 
    const f3 = ( ) => retVal; 
    console.log(f3( ));
    const f4 = _ => retVal;	// if no parameters can replace ( ) with _ 
    console.log( f4( ));
     
    //3. Returning objects
    const f5 = (param) => obj;	// no difference
    const f6 = (param) => {'prop1': 'foo', 'prop2': 'bar'}	// FAILS
    const f7 = _ => ({'prop1': 'foo', 'prop2': 'bar'})	// wrap object literals in parenthesis!