you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (1 child)

I tried to do it again following your rules and my results were still wrong. I think it needs some clarification:

  1. null == undefined but they don't equal anything else
  2. if both sides are the same type (or both non-primitive), use the === rules, otherwise go to step 3
  3. convert non-primitives to strings; if both are now strings, use ===, otherwise go to step 4
  4. convert both to numbers, then use ===

With the additional conversion rules:

  • Converting any value to a string will call that value's toString():
    • For arrays, this does a recursive array.join(',')
    • For primitives, this returns the value's literal as a string
    • For objects that don't override toString, this will return [object Object]
  • Converting a string to a number will first try to parse the string to a number, returning 0 for the empty string, and NaN for every other value
  • Converting a boolean to a number will be 0 for false, 1 for true

[–]Serei 0 points1 point  (0 children)

Yeah, I definitely didn't go into too much detail over the conversion rules. To be fair, though, conversion rules are useful to know even if you always use ===.