3

Would someone explain the difference between an undefined value and Undefined type, and similarly the same for null values and Null types?

Bonus: why do boolean, string, and number have values, types, and objects, and null and undefined have only values and types (no objects)? If "everything is an object", then one would assume we have objects for all available values, yes? Where's my Infinity and NaN objects?

Leila Hamon
  • 2,505
  • 7
  • 24
  • 32
  • The one is a type, the other is a value. There's not much to explain, I guess? – Rob W Jul 30 '12 at 12:41
  • Here you have an answer http://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript – AntonioHS Jul 30 '12 at 12:45
  • I know that an undefined value is a "primitive value used when a variable has not been assigned a value." I think @Felix_Kling hit the nail on the head below, "what is the difference between value and type"? I suppose that's what I don't know. – Leila Hamon Jul 30 '12 at 13:16

3 Answers3

3

null and undefined are values. Coincidentally (or just a wise choice), their types are null respectively undefined, as defined null value/type, undefined value/type in the specification. This can also be shown by using the typeof operator:

typeof undefined; // "undefined"
typeof null     ; // "null" (in ES6) and "object" in <=ES5 (bug)

A value is associated with a type, which indicates the "kind" of a value.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 2
    So what is the difference between value and type? (I know that you cannot compare them, but you don't really explain how they relate to each other). – Felix Kling Jul 30 '12 at 12:53
  • 2
    Note that `typeof null` still is `'object'` in ES6 as per [the spec](http://www.ecma-international.org/ecma-262/6.0/#sec-typeof-operator-runtime-semantics-evaluation). – Kyll Apr 25 '16 at 08:16
3

To give an answer to the question "What is the difference between a value and a type?":

Think of a type as some kind of category and a value is a concrete instance in that category.

For example, we have the type String and a concrete value would be "foo". There can be many different values of type String, but there are only one possible value for the Null and Undefined types, which are null and undefined respectively.

Section 4.3 of the specification is most helpful IMO. Here you find for example the information about strings and the difference between value, type and object.

4.3.16 String value
primitive value that is a finite ordered sequence of zero or more 16-bit unsigned integer.

NOTE: A String value is a member of the String type. Each integer value in the sequence usually represents a single 16-bit unit of UTF-16 text. However, ECMAScript does not place any restrictions or requirements on the values except that they must be 16-bit unsigned integers.

4.3.17 String type
set of all possible String values.

4.3.18 String object
member of the Object type that is an instance of the standard built-in String constructor.

NOTE: A String object is created by using the String constructor in a new expression, supplying a String value as an argument. The resulting object has an internal property whose value is the String value. A String object can be coerced to a String value by calling the String constructor as a function (15.5.1).

It's similar for null and undefined, though they don't have equivalent objects. Why? For that you'd have to ask those who define that language ;)

If "everything is an object", then one would assume we have objects for all available values, yes?

A more correct statement would be "nearly everything is an object", primitive values are not objects obviously, but most of them have an object equivalent (which should actually not be used) and for the others we don't care. Fact is that JavaScript is mainly object oriented.

Where's my Infinity and NaN objects?

Infinity and NaN are values of type Number, so you could create Number objects, like so:

new Number(1/0) // Infinity
new Number("a") // NaN

but you rarely use Number objects anyway.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Good morning Felix, thanks for your response. It still isn't clear to me the difference between a null value and a null type. How do you get a Javascript console to output these values? – Leila Hamon Aug 02 '12 at 13:11
  • 1
    As I said, there are various data types in JavaScript, for example strings, numbers, etc. Now, of which type would you consider the **value** `null` to be of? String? Number? If so, why? It does not seem to fit naturally to any of these types, that's why it has its own type, the `Null` type. They could have also decided to create a type named `Special` with the values `null` and `undefined`, but they didn't. You cannot list types and their values since some types (e.g. Numbers) have an infinite number of values. It does not really matter either, because in code, you only work with *values*. – Felix Kling Aug 02 '12 at 13:25
  • @Felix_Kling That response really helped clarify the distinction, thank you so much. I'm going to ask some more questions on JSMentors to really make sure I understand this concept, and I'll post the link in an edit of this comment. – Leila Hamon Aug 02 '12 at 13:49
0

typeof null === "object"

just to rule that out. null gets treated as an object. Not everything in ECMAscript is an object. The "other side" are the such called "primitive values". Primitive values are, simply spoken, plain values which were not created by any Object constructor. For instance:

var prim = 23;

prim.foo = true;
console.log( 'prim is: ', prim, 'prim.foo is: ', prim.foo );  // "prim is 23, prim.foo is: undefined"

On the other hand

var prim = new Number(23);

prim.foo = true;
console.log( 'prim is: ', +prim, 'prim.foo is: ', prim.foo );  // "prim is 23, prim.foo is: true"

Notice, that I explicitly cast the prim on the second snippet with the plus operator. The second we operate on such a "primitive value", ECMAscript (or more precisely, its engine) will convert that value into its Object representation internally anyway. So you point was actually pretty much correct, "everything is an object".


undefined is a defined value (even if it sounds silly):

'undefined' in window // true

whereas null is a type, which is just part of the language.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 4.3.11 defines the null value and 4.3.12 defines the null type. Would you mind explaining where in the standard number values are converted to number objects? I was looking at the plus operator and number sections, but couldn't find it. Thanks for taking the time to respond! – Leila Hamon Aug 02 '12 at 12:51