Article Author: Tyan
Blog: noahsnail.com | CSDN | Jane book 1. The return value of JavaScript
The return values in JavaScript are grouped into four categories:
Return
return false;
return true;
return variable (variable);
These four return values are actually very different, and the following are mainly described in the four cases. 2. return
Introduce return first, directly in code, look at the following code first:
var i= (function () {return;}) ();
alert (i);
function () {return;} is an anonymous function, (function () {return;}) It can be seen as the name of an anonymous function, similar to add in Add (), followed by () that executes the anonymous function, similar to executing the Add () function. I is the anonymous function function () {return;} Return value, note that functions have return values in JavaScript, and the default function return value is undefined. So the code above is equivalent to:
var i= (function () {}) ();
alert (i);
Equivalent to:
var i= (function () {return undefined}) ();
alert (i);
The output of Run alert (i) is undefined. From the output of the code, it can be seen that the main function of return is to prevent the function from continuing, directly returning to undefined.
Note: in javascript undefined = = NULL, note the difference between = = = = =. 3. Return False
The description of return false is directly on the code:
var i= (function () {return false;}) ();
alert (i);
The output of Run alert (i) is false. JavaScript false = = ', false = = 0,false = ' 0 ', normally, return False returns a Boolean value, or it can prevent the function from continuing. In an event function, however, return false indicates a response function that does not perform an event, for example, clicking a Button,button response function in the browser while browsing the page has return false, which means that when the button is clicked, no response is made to the Click event. 4. Return True
The introduction to return True is also on the code:
var i= (function () {return false;}) ();
alert (i);
The output of Run alert (i) is true. JavaScript true = = 1,true = = ' 1 ', normally, return True returns a Boolean value, or it can prevent the function from continuing. However, in an event function, return True has no effect, and the response function continues to execute. 5. Return variable
Return variable basically defines a variable in JavaScript and returns it in a function, no different from the usual return variable.
Summary: When writing a response function in a JS file, if you want to return TRUE or false, or define a variable, return it.