24

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,

Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
Alex Ivasyuv
  • 8,585
  • 17
  • 72
  • 90

7 Answers7

13

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.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
5

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
  • 510,854
  • 105
  • 1,084
  • 1,005
0

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
  • 10,969
  • 10
  • 47
  • 65
  • 1
    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 Mar 25 '10 at 18:50
  • 1
    Also, `null` is *not* an Object, it is a *primitive value*, sadly the `typeof` operator is just wrong... – Christian C. Salvadó Mar 26 '10 at 01:45
0

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
  • 7,150
  • 2
  • 28
  • 37
0

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
  • 405,095
  • 59
  • 585
  • 614
  • `Object.prototype.toString.call(undefined)` gives me `[object Window]` in Firefox o_O. – kennytm Mar 25 '10 at 19:04
  • 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. – Christian C. Salvadó Mar 25 '10 at 19:06
  • Right - if using that technique you'd probably do an explicit === test for undefined first – Pointy Mar 25 '10 at 19:58
0

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
  • 102,654
  • 32
  • 106
  • 127
0

There is also Array.prototype as well.

  • Object.prototype
  • Array.prototype
  • Function.prototype
  • Number.prototype
  • String.prototype
  • Boolean.prototype

Crockford says not to use:

  • new Number()
  • new String()
  • new Boolean()
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373