In ECMAScript, a variable can have two types of values, the original value and the reference value.
Raw values and reference values
In ECMAScript, a variable can have two types of values, the original value and the reference value.
-
Original value
-
Simple data segments stored in stacks (stack), that is, their values are stored directly in the location where the variable is accessed.
-
Reference value
-
the object stored in the heap (heap), that is, the value stored at the variable is a pointer (point) to the memory of the storage object.
When assigning a value to a variable, the ECMASCRIPT interpreter must determine whether the value is a primitive type or a reference type. To do this, the interpreter tries to determine whether the value is one of the original types of ECMAScript, that is, Undefined, Null, Boolean, number, and String. Because the space occupied by these primitive types is fixed, they can be stored in a smaller memory area-the stack. This stores the values that facilitate quick lookup of variables.
In many languages, strings are treated as reference types, not primitive types, because the length of a string is variable. ECMAScript has broken this tradition.
If a value is a reference type, its storage space is allocated from the heap. Because the size of the reference value changes, it cannot be placed in the stack, otherwise it will reduce the speed of the variable search. Instead, the value placed in the stack space of the variable is the address that the object stores in the heap. The size of the address is fixed, so storing it in the stack has no negative effect on the performance of the variable. As shown in the following:
Original type
As mentioned earlier, ECMAScript has 5 primitive types (primitive type), namely Undefined, Null, Boolean, number, and String. ECMA-262 defines the term type (type) as a collection of values, and each primitive type defines the range of values it contains and its literal representation.
ECMAScript provides the typeof operator to determine whether a value is within a certain type of range. You can use this operator to determine whether a value represents an original type: if it is the original type, you can also determine which primitive type it represents.
In a later chapter, we will give you an in-depth explanation of the original type and reference type of ECMAScript.
ECMAScript raw values and reference values