-6

Why does javascript treat "xy" == new String("xy") as true, but "xy" === new String("xy") as false?.

I have read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators, but am still confused

2 Answers2

4
typeof "xy"

is "string"

typeof new String("xy")

is "object"

=== compares both value and type

== converts the types and then compares just the values

Will
  • 4,299
  • 5
  • 32
  • 50
0

the == operator just compares the values, === compares values and types. So type of "xy" is string and type of new String() is object. That's why you see the difference between those two comparisons

Instance Noodle
  • 227
  • 1
  • 10