Sometimes a String is just a String. Or not.
Posted on November 4th, 2008
JavaScript, I love you, but I will never understand you. From the Firefox console:
>>> typeof 'test'
"string"
>>> 'test' instanceof String
false
>>> s = new String('test')
>>> typeof s
"object"
>>> s instanceof String
true
So the string "test" can be a "string", not a String, an "object" or a String. Good times. Then there's my personal favorite:
>>> typeof null
"object"
Null is an object? I object. In Ruby, nil is a full-blown object but in JavaScript, null is not. So when it comes to typeof, be careful what you ask for.
Douglas Crockford, perhaps the Godfather of JavaScript, comments further on typeof weirdness and offers an improved implementation.