return false:阻止默认行为,取消事件冒泡,以及停止回调执行立即返回.
return false:返回错误的处理结果, 终止处理, 阻止提交表单, 阻止执行默认的行为.
The return value of 1. Javascript
Javascript return value total into four categories:
return;
return false;
return to true;
return variable (variable);
four return values actually are very different, mainly following these four cases are described.
2. return
Introduces return;
directly with the code to illustrate, look at the following code:
var i=(function(){return;})();
alert(i); // undefined
Note: In Javascript functions return value, the function returns the default value undefined.
function () {return;} anonymous function (function () {return;}) may be regarded as anonymous function name similar to add () of the add, the latter () represents the implementation of the anonymous function, similar to implementation of add () function. i is an anonymous function function () {return;} return value.
The above is equivalent to the following:
var i = (function() {})();
alert(i);
Equivalent to:
var i = (function() {
return undefined
})();
alert(i);
Run alert (i) the output is undefined. As it can be seen from the code output, return; main role is to prevent the function proceeds directly returns undefined.
Note: In Javascript undefined == null, pay attention to the difference between == and ===.
3. return false
Return false presentation or directly on the code:
var i = (function() {
return false;
})();
alert(i); // false
Javascript, false == '', false == 0 , false == '0'.
Under normal circumstances, return false return a Boolean value, can also block the function continues.
But in the event function, return false means that no response function of the event.
For example, when browsing the browser, click a page button, button response function has a return false, which means that when click the button, do not respond to click events.
let btn1 = document.querySelector('.btn1')
btn1.onclick = function() {
console.log(111); // 111
return false;
console.log(222); // 不执行
}
4. return true
Return true on the introduction of the code is:
var i = (function() {
return true;
})();
alert(i); // true
Javascript, true == 1, true == '1 '.
Under normal circumstances, return true return a Boolean value, can also block the function continues.
But in the event function, return true not play any role in the response function will continue.
let btn2 = document.querySelector('.btn2')
btn2.onclick = function() {
console.log(333); // 333
return true;
console.log(444); // 不执行
}
5. return variable
return variable is the definition of a variable in Javascript, for the return, the return variable and usually no difference in the function.
Summary: When writing the response function in JS file, if you want to return true or false, or define the variable return it.