The return statement in JavaScript simply introduces _javascript techniques

Source: Internet
Author: User

The return statement is very important in JS, not only has the function of returning function value, but also has some special usage, the following is combined with the example to briefly introduce the function of returns statement.

A. 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 returned value after a series of code executes, and this value is returned through the return statement, and control is returned to the calling 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 ())
}

In the above code, when the Func () function is invoked, control is mastered by the Func function, and when the add function is called again, control is delivered to the Add function, then a value is returned and control is paid to the Func function.

Usually the return followed by an expression, but not absolute, for example:

Return

This situation is simply the transfer of control to the calling function to continue execution.

Extension Description:

The general application of the return statement is not special, and the most notable 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> 
 
 

Clicking on a link will cause the OnClick event to be clicked, and the default action is to link to the link to the href attribute, but the default event will be blocked if the event handler uses return FALSE.

Return false can also organize the occurrence of event bubbling, which can be read in the chapter on the role of return false in JavaScript.

Above through a concrete example to introduce the JavaScript in the return statement, I hope you like.

Related Article

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.