return statements send a value from the function, back to the function call. This is called "returning a value".
return statements are only found in the body of functions; anywhere else would be a syntax error. Here is the basic syntax of a
return keyword comes a single expression. The expression can be a variable, a literal value, or some other kind of single expression. This expression gets evaluated and its resulting value is sent back to the function call. If you need to return multiple values, use a single array.
Here is an example:
return statement in the function body will not execute.
Here is some examples of some incorrect
//you can only have a single value returned //resulting from a single expression return variable_one, variable_two; //or //you can't have a line break in between //the return keyword and your expression return [your_expression];
In the examples above, you see that you can't include multiple expressions or line breaks after your
You are also able to leave a
return statement empty, with no expression, if you do not want to return a value. Here is an example:
You also have the option of not including a
return statement in your function. With no
return statement, your function executes each statement sequentially until it reaches the end of the function body. At that point it returns the value
undefined back to the function call. The function call is also referred to as the invocation expression. In this example, the invocation expression is said to evaluate to