6 ms·
I got this in an interview once as a curveball. It's most certainly an object in Javascript! > typeof null "object" Contrast that with: > typeof undefi
by notjosh 13y ago
I got this in an interview once as a curveball. It's most certainly an object in Javascript!
> typeof null
"object"
Contrast that with:
> typeof undefined
"undefined"
Go figure :)
- saurik 13y agotypeof null returns "object", but I do not believe it is correct to claim that null "is an object". It is my understanding (maybe a misunderstanding, I will happily state) from reading a ton of comments from Brendan Eich in various places that typeof null is "object" for historical reasons involving the way reference types were implemented in the original JavaScript VM.
- inglor 13y agoExactly, if people are interested I'll gladly find the reference to the relevant esdiscuss thread.
- raganwald 13y agoIt is correct to say that typeof(null) returns the string 'object' because of a bug that is now written into the standard to prevent old software from "breaking" if they fixed this. It is not ever correct to say that null in JavaScript is an object. Just ask javaScript itself: Object.getPrototypeOf(null) //=> TypeError: Object.getPrototypeOf called on non-object
- inglor 13y agoThat's a mistake in the specification. We tried to fix that experimentally but it broke too many sites. If you check the language specification you can see that `null` is in fact not an object - it's something called a 'primitive value type', just like numbers, strings, undefined and booleans. Here http://es5.github.io/#x8 http://es5.github.io/#x8 :)
- hmsimha 13y agoCan you reconcile what you've just said with this excerpt from your guide: > There's our first brush with JavaScript objects! Did I mention that strings are objects too? They have methods as well Also, how do NaN and infinity fit in? I'm guessing they're special cases of Number types?
- inglor 13y agoGood question. First - this is _not_ my guide. MDN is a wiki and anyone can edit it. - NaN is just a number type . You have NaN in Java or C++ too. It's a way to represent "a number we can't represent" - Good catch on strings, I'll edit it in.
- ahoge 13y ago> Also, how do NaN and infinity fit in? I'm guessing they're special cases of Number types? Those are magical values from IEEE 754. IEEE 754 is also the reason why there is a signed zero or why division by zero results in +/- Infinity. >>> 0 === -0 true >>> 1/0 === 1/-0 // compares Infinity with -Infinity false
- raganwald 13y ago"hello" Not an object. new String("hello") Object. Along the way, autoboxing will convert a string to an object delegating to String.prototype if you write something like "hello".foobar(). But primitive value strings are not objects.
- raganwald 13y agoDouglas Crockford on the history of this behaviour: https://www.youtube.com/watch?v=v2ifWcnQs6M#t=489 https://www.youtube.com/watch?v=v2ifWcnQs6M#t=489
- wturner 13y agoIn JavaScript there are two instances where typeof lies. One is with null the other is with function.