爲什麼javascript將"xy" == new String("xy")
視爲true,但是"xy" === new String("xy")
爲false ?.爲什麼==不同於===這裏?
我已閱讀https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators,但我仍然感到困惑
爲什麼javascript將"xy" == new String("xy")
視爲true,但是"xy" === new String("xy")
爲false ?.爲什麼==不同於===這裏?
我已閱讀https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators,但我仍然感到困惑
typeof "xy"
是 「字符串」
typeof new String("xy")
是 「對象」
===
價值和類型比較
==
轉換類型然後比較t他的值爲
好吧。那麼這是如何回答這個問題的。 – Tushar
@Tushar對不起有推薦 – DigitalShotts
_ ===比較價值和type_你能解釋爲什麼'=='返回'true'? – Tushar
==
運算符只是比較值,===
比較值和類型。所以"xy"
的種類是string
,new String()
的種類是對象。這就是爲什麼你看到這兩個比較之間的區別
檢查'typeof「xy」'和'typeof new String(「xy」)'和'==='也會比較類型。 – Tushar
===也比較*類型*,並且它們不相同,無所謂。 – Bathsheba
請不要問重複的問題http://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons –