Return,return false and return True__javascript in JavaScript

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.