The usage and difference of Return,return True,return false in JS

Source: Internet
Author: User

1. Syntax and return mode

① return control and function results

The syntax is: return expression;

The execution of the statement result function, returning the calling function, and returning the value of the expression as a function result

② return control no function result

The syntax is: return;

In most cases, the event handler can prevent the default event behavior if it returns false. For example, by default, by clicking on a <a> tag element, the page jumps to the page specified by the href attribute of the element. and return false equals the Terminator, and return True is the equivalent of the executor. The function of return false in JS is generally used to cancel the default action. For example, if you click a link to trigger a default event in addition to triggering your "onclick" event, the jump to the page is performed. So this time, if you want to cancel the object's default action, you can return False to prevent its action. That is, if you want to use the JS code to partially change some data without causing the changes in other parts of the page, then you should add the return false after the OnClick event code;

In JS, we usually use return false to prevent the submission of the form or continue to execute the following code, in general, to prevent the execution of the default behavior.

function S1 () {    if(true) {        returnfalse;    }}
function S2 () {    m ();    n ();    P ();}

In the above two examples, the function S1 is no problem, the IF function returns false, terminates the function. And in the function s2, if we return a return false in the M function to block the commit, this does not affect the execution of function n and function p. Call the function m in the S2 function, The return false for the function S2 is equivalent to the return value, and does not prevent the function s2 from executing. Return false is valid only in the current function and does not affect the execution of other external functions.

Summarize:

return true; returns the normal processing result.

return false; Returns the processing result of the error, terminates the processing, prevents the form from being submitted, and prevents execution of the default behavior.

return; Returns control to the page.

2.

Normally a function needs to return a value to the outside after a series of processing, which is usually returned with a return, or return is the return value returned to the function and terminates the function's operation.

With regard to return, it is important to note that the content behind the return in the function is no longer executed.

functionA () {
return10;
document.write (50);//do not execute
}
A (); // Ten

In the example above, "return 10;" At this point, the value of function A is equal to 10, and the contents below the function are no longer executed because the function a below is entered with 10.

It is necessary to return a value without a return, to see what the function is doing, to write a return if it needs to return a value, or to write it if it does not need to return a value.

function A (b,c) {    return B +c;} var abc=a (5,8); Console.log (ABC); //  -

function A (b,c) {    document.write (b+c);} var abc=a (5,8); // at this point the page will output 13, but in fact ABC is no value of console.log (ABC); // undefined

Of course, the return in JS does not have to be used in function functions, and sometimes it can be used to block certain actions, such as the form's submission, so that the form's commit event returns false, the form will not be submitted: onsubmit= "return false";

Reference: http://www.cnblogs.com/Steven-Love-Arlene/archive/2012/05/23/2515273.html

The usage and difference of Return,return True,return false 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.