0

I'm trying to understand the following case:

> "1" === 1
false

> "1" == 1
true

> true === 1
false

> true == 1
true

> "true" === true
false

> "true" == true
false

Why javascript comparison ("true" == true) is false?

Also, what is the proper way to compare this one?

Teddy Sterne
  • 13,774
  • 2
  • 46
  • 51
Willian
  • 3,011
  • 1
  • 15
  • 38

2 Answers2

1

Question has been asked before here.

In essence, "true" is converted to NaN, while true is converted to 1 (which is a boolean. Hence, they differ.

cedes
  • 74
  • 2
1

The Boolean in the comparison is going to have an implicit conversion to Number (0/1) so 1=true gets true, but "true" == true (1), so the string can't be equal to Number, it gets false