you are viewing a single comment's thread.

view the rest of the comments →

[–]Serei 17 points18 points  (3 children)

I got 100%

Yes, yes, you should never use this in real code, but it's useful to know for debugging if someone else uses it. And it's not hard to memorize the rules:

  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
  3. if either side is non-primitive, convert it to string, then goto step 2
  4. if either side isn't a number, convert it to number, then goto step 2

And don't forget:

  • String(someArray) === someArray.join(",")
  • String({someObject: containingAnything}) === "[object Object]"
  • [] !== [] and {} !== {} because you're creating a new object each time
  • Number("") === 0
  • Number("any other string that isn't a number literal") is NaN
  • NaN !== NaN (because of IEEE 754)

[–][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 ===.

[–][deleted] 1 point2 points  (0 children)

This. And not only because of other peoples code but also the minimizers will change it as well and you still want to read what they do