created by Brian LeRoux & Andrew Lunny. sparodically uncurated by David Trejo.

2010 02 26 implicit tostring fun

Be careful with those implicit .toString() calls in == comparisons.

    typeof "abc" == "string"          // true
    typeof String("abc") == "string"  // true
    String("abc") == "abc"            // true -- same types get casted to equal each other    

Also, instantiation via the new operator can yield interesting results!

    String("abc") instanceof String        // false -- hmmm...
    (new String("abc")) instanceof String  // true
    String("abc") == (new String("abc"))   // true -- wait, wtf?

As always, we are reminded to test with the strict equality operator: ===.

Fork me on GitHub