Why does new Number(2) != new String(“2”) in JavaScript

前端 未结 3 1852
名媛妹妹
名媛妹妹 2021-01-18 11:06

The following evaluate to true:

new Number(2) == 2
new String(\"2\") == \"2\"

Obviously, but so do the following:



        
3条回答
  •  抹茶落季
    2021-01-18 11:47

    What I think == is basically does value comparision.

    In above all situations it's comparing just values. But in this one

    new Number(2) == new String("2")
    

    Both are objects so it doesn't compare values, it tries to compare values of object references. That's why it returns false.

提交回复
热议问题