Javascript 为什么javascript将0视为空字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12422064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why javascript treats 0 equal to empty string?
提问by netemp
Possible Duplicate:
Implied string comparison, 0='', but 1='1'
可能重复:
隐含字符串比较,0='',但 1='1'
Executing the following case in javascript, I get 0 equal to '' (an empty string)
在javascript中执行以下情况,我得到0等于''(空字符串)
var a = 0;
var b = '';//empty string
if(a==b){
console.log('equal');//this is printed in console
}else{
console.log('not equal');
}
Could anyone guide that what is the reason behind this?
任何人都可以指导这背后的原因是什么?
回答by raina77ow
Quoting the doc (MDN):
引用文档(MDN):
Equal (==)
If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible.
等于 (==)
如果两个操作数的类型不同,JavaScript 会转换操作数,然后应用严格的比较。如果操作数是数字或布尔值,则尽可能将操作数转换为数字;else 如果任一操作数是字符串,则另一个操作数在可能的情况下转换为字符串。
As a
operand type here is Number
, b
gets converted to Number as well. And Number('')
evaluates to 0
.
由于a
这里的操作数类型是Number
,也b
被转换为 Number。并Number('')
评估为0
。
This can be quite surprising sometimes. Consider this, for example:
有时这可能会令人惊讶。考虑一下,例如:
console.log(0 == '0'); // true
console.log(0 == ''); // true
console.log('' == '0'); // O'RLY?
... or this:
... 或这个:
console.log(false == undefined); // false
console.log(false == null); // false
console.log(null == undefined); // fal.... NO WAIT!
...and that's exactly why it's almost always recommended to use ===
(strict equality) operator instead.
...这正是为什么几乎总是建议使用===
(严格相等)运算符的原因。
回答by Praveen Kumar Purushothaman
0
, ""
(Empty String), false
all technically have the same value, when typecasted. If you need to strictly treat them, you can use ===
.
0
, ""
(Empty String),false
技术上所有的值在类型转换时都相同。如果你需要严格对待它们,你可以使用===
.
It is the same with similar programming languages, like PHP.
类似的编程语言,如 PHP,也是如此。
var a = 0;
var b = ''; //empty string
if(a == b){
console.log('equal'); //this is printed in console
}else{
console.log('not equal');
}
To make a strict comparison:
做一个严格的比较:
if(a === b){
console.log('equal');
}else{
console.log('not equal'); //this is printed in console
}
回答by Leri
==
operator in javascript don't compare types so 0=='' === true
(because as a number string ''
evaluates to 0) or 0==false === true
(because bool false
evaluates to 0
) to compare types to you can use ===
operator.
==
javascript 中的运算符不比较类型,因此0=='' === true
(因为数字字符串的''
计算结果为 0)或0==false === true
(因为 boolfalse
计算结果为0
)来比较类型,您可以使用===
运算符。
Hereyou'll find useful information about comparison.
您将在此处找到有关比较的有用信息。
回答by SReject
In most programming language(including javascript) ""
(string), 0
(integer), \x0
(null) losely mean the same thing: "empty". What's happening is your javascript engine finds "" == 0
false, due to the ==
it converts 0
to an integer. Again this is false, so it converts 0
to null which is false, so then it converts 0
to an empty string. (Not sure if this is the correct order of conversion). To make the condition "exact" match(no conversion) use ===
inplace of ==
在大多数编程语言(包括 javascript)中,""
(string)、0
(integer)、\x0
(null) 都表示同样的意思:“空”。发生的事情是您的 javascript 引擎发现"" == 0
错误,因为==
它转换0
为整数。这再次是假的,所以它转换0
为 null,这是假的,然后它转换0
为空字符串。(不确定这是否是正确的转换顺序)。要使条件“完全”匹配(无转换),请使用===
代替==
回答by chiborg
Javascript automatically converts variables of different types for comparison. That's a common feature in non-strictly typed languages.
Javascript 会自动转换不同类型的变量进行比较。这是非严格类型语言的一个常见特性。
If you need to compare variables and check the type, use the ===
operator.
如果需要比较变量并检查类型,请使用===
运算符。
回答by Michael Krelin - hacker
Because empty string represented as number is zero. If you compare apples and oranges you should think of how your particular orange would look if it were an apple.
因为用数字表示的空字符串为零。如果您比较苹果和橙子,您应该考虑如果它是苹果,您的特定橙子会是什么样子。
回答by skunkfrukt
Because of coercion. It is usually a better idea to use ===
for comparisons in JavaScript.
因为胁迫。===
在 JavaScript 中用于比较通常是一个更好的主意。
回答by kbec
Because ==
checks value equality so false
, 0
and empty strings are equals. Use identity check ===
.
因为==
检查值相等所以false
,0
空字符串是相等的。使用身份检查===
。
回答by Geuis
== does typecasting. Alwaysuse ===.
== 进行类型转换。总是使用 ===。
In your example, the empty string of b is being converted to 0. So both a and b are the same.
在您的示例中,b 的空字符串被转换为 0。所以 a 和 b 是相同的。