Skip to content

Instantly share code, notes, and snippets.

@getify
Created February 26, 2010 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/316141 to your computer and use it in GitHub Desktop.
Save getify/316141 to your computer and use it in GitHub Desktop.
// be careful with those implicit .toString() calls in == comparison
typeof "abc" == "string" // true
typeof String("abc") == "string" // true
String("abc") == "abc" // true -- same types get casted to equal each other
String("abc") instanceof String // false -- hmmm...
(new String("abc")) instanceof String // true
String("abc") == (new String("abc")) // true -- wait, wtf?
@goodevilgenius
Copy link

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

That's not casting.

As you can see further down String("abc") doesn't return an object that's an instance of String. You'd obviously need to do new String("abc") for that.

Without new, String("abc") simply returns a string, not an instance of String. That's why String(5) === '5'

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