2016-08-23 30 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()的種類是對象。這就是爲什麼你看到這兩個比較之間的區別

相關問題