The use of return in JS

Source: Internet
Author: User

First, return control and function results

Syntax: return expression;

Executes at the end of a function statement and returns the value of the expression as the result of the function;

Second, return control

Returns the null value, syntax: return;

In general, return Return:false for the event handler function; , the function is to block the default event behavior and cancel the default action, for example, by clicking on a <a> element by default, the page jumps to the page specified by the element href attribute, and when you use return false, it is equivalent to a terminator and return true; Equivalent to an executor.

For example: <a href= "eoh.html" onclick= "return Add_onclick ()" >open</a>//return false/true

<script>

function Add_onclick () {

return false; False to prevent jumps

}

</script>

In JS, return false is often used; To prevent the form from committing or to continue executing the following code, which is the default behavior that prevents execution.

For example: function sum () {

if (true) {

return false;

}

}

function Test () {

SUM ();

Num ();

}

Although the SUM function returns false to block commits, but does not affect the execution of the NUM function, returning false in a function is only equivalent to a return value for the test () function, and does not affect the execution of the test () function, in short, return:false; only valid for the current function. Does not affect other function executions.

The use of return in JS

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.