1. Grammar and return mode
① return control and function results
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, you can prevent the default event behavior if it returns false for the event handler. For example, by clicking a <a> tab element by default, the page jumps to the page specified by the href attribute of the element. Return false is the equivalent of a terminator, and return True is the equivalent of an operator. The effect of return false in JS is generally used to cancel the default action. For example, you click a link in addition to triggering your "onclick" event to trigger a default event is to perform a page jump. So this time, if you want to cancel the default action on the object. False to prevent it from moving. That is, if you want to use the JS code to partially change some data without causing changes to other parts of the page, then you should add return false after the OnClick event code;
In JS, we usually use return FALSE to block the submission of the form or continue with the following code, which is, in layman's terms, blocking the execution of the default behavior.
function S1 () {
if (true) {return
false;
}
}
function s2 () {
m ();
n ();
P ();
}
Above two examples, function S1 writes without question, if function body return out false, terminate function. And in the function s2, if we back a return false in the M function to block the commit, this does not affect the execution of function n and function p. Call function m in the S2 function That's return false. For function S2, it is just the equivalent of the returned value and does not prevent the execution of the function S2. Returns 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 wrong processing result, terminates processing, blocks the submission of the form, and prevents the default behavior from being performed.
Return the control to the page.
2. Normally the function after a series of processing needs to return a value to the outside, this value is generally returned with return, it can be said return is to the function returned return value, and terminate the function of the Operation .
As regards return, it is important to note that the content behind return in the function is no longer executed.
function A () {return
;
document.write (50);//Do not perform
}
a ();//10
In the example above, "return 10;" The value of function A is equal to 10 at this time, and the content below the function is no longer executed, because the following runs function A, enter 10.
There is no need to return the value, to see what the function is to do, if you need to let it return a value to write returns, if you do not need it to return the value, you do not 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);//This page will output 13, but in fact ABC is not a value of
console.log (ABC);//undefined
Of course, the return in JS does not have to be used in function functions, sometimes can also be used to prevent certain actions, such as form submission, so that the form of the submission of the event returned false, the form will not submit: Onsubmit= "return false";