Skip to content
Advertisement

Arrow function without curly braces

I’m new to both ES6 and React and I keep seeing arrow functions. Why is it that some arrow functions use curly braces after the fat arrow and some use parentheses? For example:

JavaScript

vs.

JavaScript

Advertisement

Answer

The parenthesis are returning a single value, the curly braces are executing multiple lines of code.

Your example looks confusing because it’s using JSX which looks like multiple “lines” but really just gets compiled to a single “element.”

Here are some more examples that all do the same thing:

JavaScript

You will also often see parenthesis around object literals because that’s a way to avoid the parser treating it as a code block:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement