The new es6 arrow functions say return
is implicit under some circumstances:
The expression is also the implicit return value of that function.
In what cases do I need to use return
with es6 arrow functions?
The new es6 arrow functions say return
is implicit under some circumstances:
The expression is also the implicit return value of that function.
In what cases do I need to use return
with es6 arrow functions?
Jackson has partially answered this in a similar question:
I would add to this the definition of a block:
Examples:
Arrow functions allow you to have an implicit return: values are returned without having to use the
return
keyword.It works when there is a on-line statement in the function body:
Another example, returning an object (remember to wrap the curly brackets in parentheses to avoid it being considered the wrapping function body brackets):
I understand this rule-of-thumb ...
Candidates are:
For other operations (more than one-liners that require a block, return has to be explicit
There's another case here.
When writing a functional component in React, you can use parentheses to wrap implicitly returned JSX.