Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 181 characters in body
Source Link
Jonas Wilms
  • 139.4k
  • 20
  • 164
  • 164

The equality operator performs a series of type coercions in case the types mismatch, this is sometimes counterintuitive. As a rule of thumb, values of different types are usually converted to a number before comparing them again as numbers:

false == "0"
0 == "0"
0 == 0
true

As specified:

If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

——-

The rules of == have little to do with truthiness - to observe whether a value would be truthy or falsy, use !!(<expression>) or Boolean(...) which forces a ToBoolean(...) conversion.

The equality operator performs a series of type coercions in case the types mismatch, this is sometimes counterintuitive. As a rule of thumb, values of different types are usually converted to a number before comparing them again as numbers:

false == "0"
0 == "0"
0 == 0
true

As specified:

If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

The equality operator performs a series of type coercions in case the types mismatch, this is sometimes counterintuitive. As a rule of thumb, values of different types are usually converted to a number before comparing them again as numbers:

false == "0"
0 == "0"
0 == 0
true

As specified:

If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

——-

The rules of == have little to do with truthiness - to observe whether a value would be truthy or falsy, use !!(<expression>) or Boolean(...) which forces a ToBoolean(...) conversion.

Source Link
Jonas Wilms
  • 139.4k
  • 20
  • 164
  • 164

The equality operator performs a series of type coercions in case the types mismatch, this is sometimes counterintuitive. As a rule of thumb, values of different types are usually converted to a number before comparing them again as numbers:

false == "0"
0 == "0"
0 == 0
true

As specified:

If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

lang-js

AltStyle によって変換されたページ (->オリジナル) /