Javascript JS 中 == 和 === 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5323377/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:40:31  来源:igfitidea点击:

Difference between == and === in JS

javascriptcomparison-operatorsequality-operatoridentity-operator

提问by jslearner

Possible Duplicates:
Difference between == and === in JavaScript
Javascript === vs == : Does it matter which “equal” operator I use?

可能的重复:
JavaScript 中 == 和 === 之间的区别
Javascript === vs == :我使用哪个“相等”运算符重要吗?

What's the difference between ==and ===? Also between !==and !==?

==和 和有===什么区别?也介于!==和之间!==

回答by jAndy

There are lots of answers to this question on Stackoverflow already.

这个问题在 Stackoverflow 上已经有很多答案了。

Short:

短的:

==only compares values

==只比较值

===compares values + type

===比较值 + 类型



var check1 = '10',
    check2 = 10;

check1 == check2 // true
check1 === check2 // false

回答by Rob Levine

"==" means equals, whereas "===" means identically equal.

“==”表示相等,而“===”表示完全相等。

In short, "==" will try and coerce/convert the types of values when doing a comparison, so "2"==2, whereas "===" will not.

简而言之,“==”将在进行比较时尝试强制/转换值的类型,因此“2”==2,而“===”则不会。