Primitive Types and Reference Types
The next topic we need to consider is the
content of variables. We often say that variables have or contain
values. But just what is it that they contain? To answer this
seemingly simple question, we must look again at the data types
supported by JavaScript. The types can be divided into two groups:
primitive types and reference types. Numbers, boolean values, and the
null and undefined types are primitive. Objects,
arrays, and functions are reference types.
A primitive type has a fixed size in memory. For example, a number occupies eight bytes of memory, and a boolean value can be represented with only one bit. The number type is the largest of the primitive types. If each JavaScript variable reserves eight bytes of memory, the variable can directly hold any primitive value.[13]
Reference types are another matter, however. Objects, for example, can be of any length -- they do not have a fixed size. The same is true of arrays: an array can have any number of elements. Similarly, a function can contain any amount of JavaScript code. Since these types do not have a fixed size, their values cannot be stored directly in the eight bytes of memory associated with each variable. Instead, the variable stores a reference to the value. Typically, this reference is some form of pointer or memory address. It is not the data value itself, but it tells the variable where to look to find the value.
The distinction between primitive and reference types is an ...