The return statement for JavaScript is a simple introduction:
The return statement is very important in JS, not only has the function of returning function value, but also has some special usages, it is very necessary to have a clear grasp. The following is a simple example of the return statement to explain the role.
One. Used to return control and function results:
In general, a return statement is necessary for a function, because it is often necessary for a function to get a desired return value after a series of code execution, and this value is returned by a return statement, and control is returned to the keynote function.
Syntax format:
return expression
The code example is as follows:
function Add () { var a=1; var b=2; return A +b;} function func () { Console.log (Add ())}func ();
In the above code, when the Func () function is called, control is mastered by the Func function, and when the add function is called, control is delivered to the Add function, and then a value is returned and the control is then handed over to the Func function.
Normally, return is followed by an expression, but not absolute, for example:
return;
This situation is simply to transfer control to the keynote function to continue execution.
Extension Description:
The normal application of the return statement has no special place, and the most important thing to note is the use of return false. The event handler function returns false to organize the occurrence of the default event.
The code example is as follows:
<!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <Metaname= "Author"content= "http://www.51texiao.cn/" /> <title>Ant Tribe</title> <Scripttype= "Text/javascript">window.onload=function(){ varOlink=document.getElementById ("Thelink"); Olink.onclick=function(){ return false } } </Script> </Head> <Body> <ahref= "Http://www.softwhy.com"ID= "Thelink">Ant Tribe</a> </Body> </HTML>
The OnClick event occurs when a link is clicked, and its default action is to link to the link specified by the href attribute, but if the event handler uses return FALSE, the default event will be blocked.
Return false can also organize the occurrence of event bubbling phenomena.
The original address is: http://www.51texiao.cn/javascriptjiaocheng/2015/0427/221.html
A simple introduction to the return statement of JavaScript