Example of a wonderful false value in JavaScript

Source: Internet
Author: User

It is usually necessary to determine the true or false in the following statement Structure

If branch statement
While loop statement
The second statement in

For example
Copy codeThe Code is as follows:
If (boo ){
// Do something
}

While (boo ){
// Do something
}

The six values in JavaScript are "false", and these six values are

False
Null
Undefined
0
''(Null String)
NaN

Here, false is a boolean type, but the other five are not.

In addition to these six, all others are "true", including objects, arrays, regular expressions, and functions. Note that '0', 'null', 'false', {}, and [] are also true values.

Although all these six values are "false", they are not all equal.
Copy codeThe Code is as follows:
Console. log (false = null) // false
Console. log (false = undefined) // false
Console. log (false = 0) // true
Console. log (false = '') // true
Console. log (false = NaN) // false

Console. log (null = undefined) // true
Console. log (null = 0) // false
Console. log (null = '') // false
Console. log (null = NaN) // false

Console. log (undefined = 0) // false
Console. log (undefined = '') // false
Console. log (undefined = NaN) // false

Console. log (0 = '') // true
Console. log (0 = NaN) // false

For "=", the following conclusions are drawn:

False: in addition to being true, and 0, ''is true.
Null is true only when compared with undefined. In turn, undefined is true only when compared with null.
The value of 0 is true in addition to false. There is also an empty string''
The Null String ''is true when it is outputted and false, and there is a number 0.

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.