【发布时间】:2012-04-29 21:34:22
【问题描述】:
今天在用==做实验的时候,无意中发现"\n\t\r" == 0。 "\n\t\r" 到底如何等于 0 或 false?
我所做的是:
var txt = "\n"; //new line
txt == 0; //it gives me true
这真的让我很恼火。所以我做了更多:
var txt = "\r"; //"return"
txt == 0; //true
var txt = "\t"; //"tab"
txt == 0; //true
这根本没有意义。这是怎么回事?更疯狂的是:
//Checking for variable declared or not
var txt ="\n\t\r";
if(txt!=false){
console.log("Variable is declared.");
}else{
console.log("Variable is not declared.");
}
它给我的是Variable is not declared.
怎么等于0,或者false???
【问题讨论】:
-
我会说“因为字符串是空的”
-
@Mehrdad - 哈哈,它用 JavaScript 写着
0.1 + 0.2 --> 0.30000000...4我不相信,我试过了,我得到了同样的结果。以前从未注意到它! -
@Derek:这是您在使用 IEEE 浮点数的每种语言中都会看到的一些行为(几乎是所有语言)
标签: javascript