JavaScript 中的 == 和 === 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8132353/
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
What is the difference between == and === in JavaScript?
提问by Exception
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
When would JavaScript == make more sense than ===?
可能的重复:
Javascript === vs ==:我使用哪个“相等”运算符重要吗?
JavaScript == 什么时候比 === 更有意义?
What is the difference between below methods in comparing a string with undefined value.
以下方法在将字符串与未定义值进行比较时有什么区别。
var x;
if(x==undefined)
{
alert(x);
}
and
和
if(x===undefined)
{
alert(x);
}
Why should i prefer second method in this case.. Please let me know advantages..
为什么在这种情况下我更喜欢第二种方法..请让我知道优点..
回答by Facebook Staff are Complicit
==
attempts to convert the values to the same type before testing if they're the same."5" == 5
===
does not do this; it requires objects to be of the same type to be equal."5" !== 5
==
在测试它们是否相同之前尝试将值转换为相同的类型。"5" == 5
===
不这样做;它要求对象的类型相同才能相等。"5" !== 5
In this case, the result is:
在这种情况下,结果是:
x == undefined
will be true ifx
isundefined
ornull
.x === undefined
will only be true ifx
isundefined
.
x == undefined
如果x
是undefined
或,则为真null
。x === undefined
只有当x
是时才会为真undefined
。
You should prefer the first method if you'd like undefined and null to be treated equivalently. One common use of this is optional function arguments.
如果您希望 undefined 和 null 被同等对待,您应该更喜欢第一种方法。它的一种常见用途是可选的函数参数。
function greet(name, greeting) {
if (name == undefined) name = 'World';
if (greeting == undefined) greeting = 'Hello';
alert(greeting + ' ' + name);
}
greet(); // alerts "Hello World"
greet("Bob"); // alerts "Hello Bob"
greet(null, "Goodbye"); // alerts "Goodbye World"
回答by Hitu Bansal
suppose we have x=5,
假设我们有 x=5,
== is equal to
== 等于
x==8 is false x==5 is true
x==8 是假 x==5 是真的
=== is exactly equal to (value and type)
=== 完全等于(值和类型)
x===5 is true x==="5" is false
x===5 为真 x==="5" 为假
Hope you understand this concept
希望你理解这个概念
回答by gjohn
== is just comparing the two values, and if they are of different types, type conversion is done
==只是比较两个值,如果是不同的类型,就进行类型转换
=== compares the values and well as their types - so no type conversion will be done here.
=== 比较值及其类型 - 所以这里不会进行类型转换。
回答by Some Guy
===
checks for the same type as well. You'll understand with a few examples:
===
也检查相同的类型。举几个例子你就明白了:
(1 == '1') //Returns true
Since ==
doesn't bother with types, that returns true. However, if you want strict type checking, you'd use ===
because that returns true onlyif the it's of the same type, and is the same value.
由于==
不关心类型,因此返回 true。但是,如果你想要严格的类型检查,你会使用===
因为只有当它是相同的类型并且是相同的值时才返回 true 。
(1 === '1') //Returns false
(1 === 1) //Returns true
- Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding
positions.- Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything,
including NaN. Positive and negative zeros are equal to one another.- Two Boolean operands are strictly equal if both are true or both are false.
- Two objects are strictly equal if they refer to the same Object.
- Null and Undefined types are == (but not ===).
- 当两个字符串具有相同的字符序列、相同的长度并且在相应
位置具有相同的字符时,它们是严格相等的。- 当两个数字在数值上相等(具有相同的数值)时,它们是严格相等的。NaN 不等于任何东西,
包括 NaN。正零和负零彼此相等。- 如果两个布尔操作数都为真或都为假,则两个布尔操作数严格相等。
- 如果两个对象引用同一个对象,则它们严格相等。
- Null 和 Undefined 类型是 ==(但不是 ===)。