True and false values in ECMAScript

If the value is null, undefined, 0, empty string, and false, the value is false, and all other values are true. That’s certainly true, but it’s not good for beginners to understand. In fact, by learning true and false values, it is the time to string together a lot of knowledge.

Read a lot of books and online articles about booleans in ECMAScript, all of which are dry sentences: if the values are null, undefined, 0, empty string, and false, the value is false, and all other values are true. That’s certainly true, but it’s not good for beginners to understand. In fact, by learning true and false values, it is the time to string together a lot of knowledge.

Here I have prepared some exercises for true and false values. Please write down the corresponding value true or false:

null undefined '' "" " ".trim() NaN {}.notHere (function () { } ()) (function () { return undefined } ()) {} [] "NaN" "False" 0-0 0.0-0.0 () => undefined {}.__proto__ NaN.__proto__ {undefined} {undefined: undefined } Symbol() Symbol('') new Boolean("false") new Boolean(false) new Boolean() Boolean("false") Boolean(NaN) (new  Boolean()).valueOf()Copy the code

The defined value of false

The following 6 values are, of course, false. If they are equivalent to these values, they are also false. Otherwise, they are all true.

null
undefined
''
""
NaN
0
false
Copy the code

Like this:

" ".trim()
Copy the code

If you remove the space, you get an empty string, which is exactly the same, no objection.

The next two strings are not empty and have no type conversion function, so they are true.

"NaN"
"false"
Copy the code

digital

Now, the fun begins. There are two things you need to learn if you are coming from another language like Java or C:

  • ECMAScript has no integers, they are floating point numbers. So 0.0 and 0 are the same thing and both are false. Either === or object. is is the same thing.
  • In ECMAScript, +0 and -0 are sometimes the same and sometimes different. 0===-0 is true, but object. is(0,-0) is not.

When taking Boolean values, we use === logic, where +0,-0,+0.0,-0.0 are all false.

object

{}.notHere
Copy the code

Undefined property, undefined, there’s nothing to say about that.

{}
[]
Copy the code

The above two objects are null objects with no defined properties or methods, but they are not null and are not undefined

NaN.__proto__
Copy the code

NaN is also an instance of the Number object, whose prototype value, although 0, is a non-empty object and therefore true.

() => undefined
{}.__proto__
{ undefined }
{ undefined: undefined }
Copy the code

The first expression above, a lambda expression, is also a function object that returns undefined but is itself a non-empty function object. The next three are also non-empty objects

function

(function () { } ())
(function () { return undefined } ())
Copy the code

The above two are equivalent, both are the result of the evaluation of undefined.

symbol

Notation is a new data type, a seventh data type, and therefore cannot be equivalent to the six cases defined above. It is safe to assume that all of them are true.

The Boolean object

Note that a Boolean object is not equivalent to any of the six false cases above; it is a valued object, so all are true. What if you want to use the value of a Boolean object? Boolean. Prototype. The valueOf () can be converted into value.

So all of the following are true:

new Boolean("false")
new Boolean(false)
new Boolean()
Boolean("false")
Copy the code

The next two are false. Boolean functions convert other values to Booleans rather than generate Boolean objects, so they can be false.

Boolean(NaN)
(new Boolean()).valueOf()
Copy the code

Use the cloud habitat community APP, comfortable ~

[Cloud Computing News] Cloud computing live channel online, come here to watch ali technology experts to share! For details, please click