2016-08-23 59 views
-6

为什么javascript将"xy" == new String("xy")视为true,但是"xy" === new String("xy")为false ?.为什么==不同于===这里?

我已阅读https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators,但我仍然感到困惑

+0

检查'typeof“xy”'和'typeof new String(“xy”)'和'==='也会比较类型。 – Tushar

+0

===也比较*类型*,并且它们不相同,无所谓。 – Bathsheba

+4

请不要问重复的问题http://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons –

回答

4
typeof "xy" 

是 “字符串”

typeof new String("xy") 

是 “对象”

===价值和类型比较

==转换类型然后比较t他的值为

+3

好吧。那么这是如何回答这个问题的。 – Tushar

+0

@Tushar对不起有推荐 – DigitalShotts

+1

_ ===比较价值和type_你能解释为什么'=='返回'true'? – Tushar

0

==运算符只是比较值,===比较值和类型。所以"xy"的种类是stringnew String()的种类是对象。这就是为什么你看到这两个比较之间的区别