The usage and difference between return, return true, return false in javascript.

Source: Internet
Author: User

The usage and difference between return, return true, return false in javascript.

1. syntax and Return Method

① Return control and function results

Syntax: return expression;

Statement result function execution, return the call function, and return the value of the expression as the function result

② Return control no function results

Syntax: return;

In most cases, if the event handler returns false, the default event behavior can be prevented. for example, by default, click a <a> label element to go to the page specified by the href attribute of the element. return false is equivalent to the Terminator, and return true is equivalent to the executable. in js, return false is generally used to cancel the default action. for example, if you click a link to trigger your "onclick" event, you also need to trigger a default event to execute the page Jump. so if you want to cancel the default action of an object, you can return false to stop the action. that is to say, if you want to use js Code to partially change some data without causing changes to other parts of the page, you should add return false after The onclick Event code;

In js, we usually use return false to block the submission of forms or the execution of the following code. In general, it is to block the execution of default behaviors.

function s1(){  if(true){    return false;  }}function s2(){  m();  n();  p();}

In the above two examples, there is no problem in writing function s1. if function bodies return false to terminate the function. in function s2, if we return a return false value in function m to prevent submission, this does not affect the execution of function n and function p. call the function m in the s2 function, where return false is equivalent to the return value for function s2 and does not prevent function s2 from being executed. return false is valid only for the current function and does not affect the execution of other external functions.

Summary:

Return true; return the normal processing result.

Return false; return the error processing result; terminate the processing; Block Form submission; prevent default execution.

Return; return the control to the page.

2. generally, a function needs to return a value to the outside after a series of processing. This value is usually returned using return. It can also be said that return is returned to the function and the function is terminated.

Note that the content after return in the function is no longer executed.

Function a () {return 10; document. write (50); // not executed} a (); // 10

In the above example, "return 10;" at this time, the value of function a is equal to 10, and the content below the function is not executed, because input 10 when function a is run below.

Whether a return value is required or not depends on the function. If a return value is required, the return value is written. If a return value is not required, it is not required.

Function a (B, c) {return B + c;} var abc = a (5, 8); console. log (abc); // 13 function a (B, c) {document. write (B + c);} var abc = a (5, 8); // at this time, 13 will be output on the page, but abc is actually a console with no value. log (abc); // undefined

Of course, the return in js does not have to be used in function functions. Sometimes it can be used to block certain actions, such as submitting a form, so that the form will not be submitted if the form submission event returns false: onsubmit = "return false ";

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.