Home > Web Front-end > JS Tutorial > Javascript Boolean Analysis_Basic Knowledge

Javascript Boolean Analysis_Basic Knowledge

WBOY
Release: 2016-05-16 18:57:25
Original
914 people have browsed it

Originally, I thought that the Boolean conversion of js was the same as that of java. After all, we all have the same ancestor. Here is how I wrote it.
function foo() {
var temp = Boolean.valueOf('');
alert(temp == false);
}
java variable javaBoolean is a string , its value is "false". Originally, I thought it would definitely output true, but the result is false, which is really frustrating.
I just checked "The Definitive Guide to JavaScript" and I suddenly had an idea. It turns out to be like this:
If you want to convert other types to Boolean, you should use Boolean(value) or new Boolean(value). The Boolean.valueOf() method is unique to objects and is not a static method of Boolean. method, there is another important point: 0, NaN, null, empty string and undefined will be converted to false, and other primitive values, except false (but containing the string "false"), as well as other objects and arrays will be converted is true.
Seeing this, you should think that being fooled by js is worth it, right?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template