Table of contents
What is a return statement?
returnstatement ends function execution and specifies a value to be returned to the function caller.
A basic example of a return is one used to stop function execution.
Note: This return statement renders the code beneath it unreachable and is solely for example purposes.
This function will only log
"Hello" to the console because the
return prevents it from getting any further.
An explicit return is one which uses the
return keyword explicitly, hence its name.
Explicit returns are used within the body of a function like so:
Let's see what an explicit return looks like with a simple arrow function.
Note: This can be written on a single line, but we'll use multiple lines for this example.
And now let's look at what an explicit return looks like within a
Now, let's take a look at these same examples using implicit returns.
Our first example can be rewritten to omit the curly braces and the return statement.
This is called an implicit return (because it returns without explicitly using a
return statement) and looks like this:
Now, let's look at how we can clean up our
map callback using an implicit return:
You may be wondering how to implicitly return an object as curly braces are also used to surround function bodies.
We can do this by wrapping our object in parenthesis:
map in the return statement of their function component.
Here's an example of this:
Note that as the component is inside a function body, so this will not work. If you are inside a function body, it must be explicitly returned like so:
Or implicitly returned by replacing the curly braces with parenthesis: