Return / Return Value

By: Dusty Arlia
Published on September 9, 2012
Last Updated in Tuesday, July 14, 2015 at 5:59 PM
Total Updates: 5

Many functions are said to "return" a value. What this means is that when the function is done running, it sends some information back to the main part of your JavaScript program. More specifically, the function call/invocation expression receives the returned value. Usually the value that is returned is stored in a variable. The value being returned is often referred to as the "returned value".

Here is how it would look like:

function functionName {
//JavaScript function code
return example_variable;

example_variable is typically a variable, but could be a value. The reason that you usually use a variable is because knowing the actual return value ahead of time is unlikely. The return statement only sends the value of example_variable. The return statement should typically be the last statement in a function most of the time. As soon as the JavaScript interpreter encounters the return statement, it exits the function. Any code after the return statement in the function is never executed. A function can only return one value. If you want to return multiple items, store the results in an array, and return the array.