The usage and distinction of return return true and return false in javascript

  • 2020-11-18 05:15:38
  • OfStack

1. Syntax and return method

Returns control and function results

The syntax is :return expression;

Statement results in the execution of a function, returns the calling function, and returns the value of the expression as the result of the function

(2) Return control no function results

Grammar: return;

In most cases, returning false to an event handler prevents the default event behavior. For example, by default, click 1 < a > Tag element, the page jumps to the page specified by the element's href attribute. return false is the terminator and return true is the executor. The role of return false in js 1 is generally used to cancel the default action. Let's say you click on a link and in addition to triggering your "onclick" event there's also a default event which is to perform a page jump. So if you want to cancel the default action of the object, you can block it by return false. That is, if you want to use the js code to change 1 bit of data locally without changing the rest of the page, you should add return false to the onclick event code.

In js, we usually use return false to prevent form submission or to continue executing the following code, which is colloquial for preventing default behavior.


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

In the above two examples, the function s1 is written the way it should be, and if returns false from the body, terminating the function. In function s2, if we return an return false in the m function, the commit is blocked, but this does not affect the execution of functions n and p. Call the function m in s2, where return false is the return value of s2 and does not prevent the execution of s2.return false is only valid in the current function and does not affect the execution of other external functions.

Conclusion:

return true; Returns normal processing results.

return false; Returns the processing result of the error; Termination treatment; Prevent form submission; Prevents the default behavior from being executed.

return; Returns control to the page.

2. Usually, after the function has been processed in series 1, it needs to return a value to the outside, which is usually returned by return. It can also be said that return returns a return value to the function and terminates the function.

The thing to note about return is that the content after return in the function is no longer executed.


function a(){ 
  return 10; 
  document.write(50);// Does not perform  
}
a();//10

In the above example,"return 10;" At this point, the value of the function a will be equal to 10, and the contents below the function will no longer be executed, because 10 was entered when the function a was run.

Whether return is needed to return a value depends on what the function is doing. If you want it to return a value, write return. If you don't want it to return a value, you don't have to write it.


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);// Output will appear on the page 13, But in fact abc It has no value 
console.log(abc);//undefined

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


Related articles: