ECMAScript raw values and reference values

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.