A simple introduction to the return statement of JavaScript

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.