As you'll recall, function invocation with the () operator is an expression. All expressions have values, and the return statement is used to specify the value "returned by" a function. This value is the value of the function invocation expression. The syntax of the return statement is:
return [ expression ];
When the return statement is executed, the expression is evaluated, and returned as the value of the function. Execution of the function stops when the return statement is executed, even if there are other statements still remaining in the function body. The return statement can be used to return a value like this:
function square(x) { return x*x; }
The return statement may also be used without an expression to simply terminate execution of the function without returning a value. For example:
function display_object(obj) { // first make sure our argument is valid // and skip rest of function if it is not. if (obj ==index.html null) return; // rest of the function goes here... }
If a function executes a return statement with no expression, or if it never executes a return statement (i.e., it simply executes all the statements in the body and implicitly returns) then the value of the function call expression will be undefined (i.e., the special JavaScript undefined value).
It is a syntax error to use the return statement anywhere except in a function body.
file: /Techref/language/JAVA/SCRIPT/definitive/ch05_12.htm, 5KB, , updated: 2019/10/14 15:00, local time: 2024/12/19 05:01,
3.144.107.32:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/techref/language/JAVA/SCRIPT/definitive/ch05_12.htm"> [Chapter 5] 5.12 return</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.