1. Syntax and return method
①Return control and function results
The syntax is: return expression Formula;
The execution of the statement result function returns the calling function, and the value of the expression is returned as the function result
②Return control function-free result
The syntax is: return;
In most cases, if you allow the event handler to return false, you can prevent the default event behavior. For example, by default, when you click an tag element, the page will jump. 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 executor. The function of return false in js is generally used to cancel the default action. For example, when you click a link, in addition to triggering In addition to your "onclick" event, you also need to trigger a default event that executes the page jump. So at this time, if you want to cancel the default action of the object, you can return false to prevent its action. In other words, if you want to use js code To locally change some data without causing changes in other parts of the page, then you should add return false after the onclick event code;
In js, we usually use return false to prevent form submission or continuation Executing the following code, in layman's terms, prevents the execution of the default behavior.
function s1(){ if(true){ return false; } } function s2(){ m(); n(); p(); }
In the above two examples, there is no problem in writing function s1 like this, and the if function body returns false, terminates the function. In function s2, if we return a return false in function m, it prevents submission, but this does not affect the execution of function n and function p. When function m is called in function s2, the return false inside For function s2, it is just equivalent to the return value and does not prevent the execution of function s2. Return false is only valid in the current function and will not affect the execution of other external functions.
Summary:
return true;Return normal processing results.
return false;Return incorrect processing results;Terminate processing;Prevent form submission;Prevent execution of default behavior.
return;Return control Return to the page.
2. Usually the function needs to return a value to the outside after a series of processing. This value is usually returned with return. It can also be said that return is returned to the function. Return the value and terminate the running of the function.
Regarding return, it should be noted that the content after return in the function will no longer be executed.
function a(){ return 10; document.write(50);//不执行 } a();//10
In the above example, "return 10;" at this time the value of function a will be equal to 10, and the content below the function will no longer be executed, because 10 is entered when running function a below.
Whether return is needed to return a value depends on what the function is used for. If you need it to return a value, write return. If you don't need it to return a value, there is no need 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);//此时页面上会输出13,但其实abc是没有值的 console.log(abc);//undefined
Of course, return in js does not have to be used in functions. Sometimes it can also be used to prevent certain actions, such as form submission. If the form submission event returns false, then the form will Will not submit:onsubmit="return false";
The above is the detailed content of Detailed explanation of the difference in usage of return value in JavaScript. For more information, please follow other related articles on the PHP Chinese website!