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.
5 Responses
I just don’t understand the last two paragraphes, can anybody help?
I know you, man, lol
I think other website owners should take this website as an model, very clean and superb user pleasant design and style . “It is the sign of a week mind to be unable to bear wealth.” by Seneca.
Do you like quotes, then you should check out Alcohol Quotes
This is precisely the important information I’d been searching for.