Skip to content

Instantly share code, notes, and snippets.

@amysimmons
Last active January 7, 2024 11:57
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amysimmons/d85e23638df02d43067633e6837414dd to your computer and use it in GitHub Desktop.
Save amysimmons/d85e23638df02d43067633e6837414dd to your computer and use it in GitHub Desktop.
false == "0" // true
// false gets coerced to 0
// "0" gets coerced to 0
// 0 == 0 is true 

false == 0 // true
// false gets coerced to 0
// 0 == 0 is true

false == "" // true
// false gets coerced to 0
// "" gets coerced to a number, 0
// 0 == 0 is true

false == [] // true
// false gets coerced to 0
// [] is an object so ToPrimitive would be called on it
// valueOf() would be consulted to find a primitive
// [].valueOf() would result in [], not a primitive value
// so [].toString() would be called, resulting in ""
// "" would then get coerced to a number, 0
// 0 == 0 is true

"" == 0 // true
// "" gets coerced to a number, 0
// 0 == 0 is true

"" == [] // true
// [] is an object so ToPrimitive would be called on it
// valueOf() would be consulted to find a primitive
// [].valueOf() would result in [], not a primitive value
// so [].toString() would be called, resulting in ""
// "" == "" is true

0 == [] // true
// [] is an object so ToPrimitive would be called on it
// valueOf() would be consulted to find a primitive
// [].valueOf() would result in [], not a primitive value
// so [].toString() would be called, resulting in ""
// "" would then get coerced to a number, 0
// 0 == 0 is true

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment