views:

185

answers:

6
+17  Q: 

JavaScript types

Hi,

As per http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf, JavaScript has 6 types: undefined, null, boolean, string, number and object.

var und;
console.log(typeof und); // <-- undefined

var n = null;
console.log(typeof n); // <--- **object**!

var b = true;
console.log(typeof b); // <-- boolean

var str = "myString"
console.log(typeof str); // <-- string

var int = 10;
console.log(typeof int); // <-- number

var obj = {}
console.log(typeof obj); // <-- object

Question 1:

Why is null of type object instead of null?

Question 2:

What about functions?

var f = function() {};
console.log(typeof f); // <-- function

Variable f has type of function. Why isn't it specified in the specification as a separate type?

Thanks,

+4  A: 

It's because typeof is defined to return "object" if the input is null, and return "function" if the input is callable. (See 11.4.3 The typeof Operator.)

I don't know why the standard is defined like this (and Crockford said it's wrong). Maybe backward compatibility.

KennyTM
A: 

Answer to Question 1:

A property, when it has no definition, is undefined. The reason null is an Object is so that a property can exist with no value yet still have a definition.

Todd Moses
Actually I think that's kind of misleading. There is a difference between undefined and undeclared. For instance, `var x` will put a property named `x` in the current scope, but its value will be `undefined`. Before this happens, `x` itself is undefined, and trying to use it will result in a `ReferenceError`.
bcherry
Also, `null` is *not* an Object, it is a *primitive value*, sadly the `typeof` operator is just wrong...
CMS
A: 

typeof null === "object" because the spec says so, but this is a mistake from the very first version of JavaScript. (as KennyTM says above).

typeof f === "function" because, without a try/catch, there is no other reliable, foolproof way to determine if something is callable. Using f.constructor === Function might work, but I think it's not guaranteed to be so.

bcherry
+8  A: 

About typeof null == 'object', this is a mistake that comes since the early days, and unfortunately this mistake will stay with us for a long time, it was too late to be fixed in the ECMAScript 5th Edition Specification.

About the functions, they are just objects, but they have an special internal property named [[Call]] which is used internally when a function is invoked.

The typeof operator distinguish between plain objects and functions just by checking if the object has this internal property.

CMS
A: 

For completeness, note that the current best-practice way to check type information is something like this:

var typeInfo = Object.prototype.toString.call(yourObject);

That gives you a string that looks like "[object Something]", where "Something" is a type name.

Pointy
`Object.prototype.toString.call(undefined)` gives me `[object Window]` in Firefox o_O.
KennyTM
KennyTM: That's because when `null` or `undefined` are used as the first argument of `call` or `apply`, the context (the `this` keyword) inside the invoked function will be set to the global object.
CMS
Right - if using that technique you'd probably do an explicit === test for undefined first
Pointy
A: 

null is a special value- it is not false, it is not 0, or the empty string or NaN or undefined.

null is what you get when you look for an object that is not there- not an undefined property of an object, but the thing itself.

a paragraph with one textNode will return null for the nodes nextSibling, a regexp that does'n match returns null instead of the array and so on.

maybe it should have its own type, but then it starts to be something, a something with a type, instead of the absence of an object.

kennebec