Search code examples
javascriptecmascript-6arrow-functionsecmascript-2017

Arrrow functions in JavaScript ES6 - with and without curly braces


Like to ask about the differences in ES6 in function syntax - with and without curly braces.

both functions are working:

  1. function with a curly braces:

    const function = () => {some code;};
    
  2. same function without curly braces:

    const function = () => some code;
    

Thanks.


Solution

  • welcome to Stackoverflow!

    Indeed these functions without curly braces are a shorthand version that differs in some nuances.

    the most important differences are:

    • they can have only one statement. (e.g. () => 20 * 5)
    • they automatically return the value of that statement (above example would return the value 100)

    Sticking to the example above, the more classic version to write this would be () => {return 20 * 5}

    more details can be found here for example.