HTML and CSS Reference
In-Depth Information
Figure 7.8
Output from Example 7.5.
Functions can return values with a
return
statement. The
return
keyword is optional and
can only exist within a function. When the
return
keyword is reached in a function, no
further processing within the function occurs. A
return
can be used to send back the
result of some task, such as a calculation, or to exit a function early if some condition
occurs. If a function doesn't have a
return
statement, it returns the
undefined
value.
FORMAT
return;
return expression;
EXAMPLE
function sum (a, b) {
var result= a + b;
return result;
}
If the call to the function is made part of an expression, the returned value can be
assigned to a variable. In Example 7.6 the
sum
function is called with two arguments,
5
and
10
. The
sum
function's return value will be assigned to the variable
total
.
var total=sum(5, 10);