JavaScript data Types-value types and reference types

Source: Internet
Author: User

Value Type: also known as raw data or original value (primitive value).

This type of value is stored in the stack (stack), the stack is a special data structure in memory, also known as a linear table, the stack in accordance with the principle of last-in-first-out to store data, the first incoming data is pressed into the bottom of the stack, the final insertion (push) of the data on the top of the stack, need to That is, the last data is read by the first One. therefore, The value types are simple data segments. The position of the variable and the position of the variable value overlap, meaning that the data of the value type is stored where the variable is Accessed.

Reference types: These values are stored in the heap, which is a dynamic area in memory, equivalent to the retention space, which is dynamically assigned to code and stacks during program Runs.

The heap is typically stored in objects and then passed through a number to the In-stack variable, which is the so-called reference pointer (point), so that the variables and variable values are separated, and they are linked by Pointers. When the data is read and written, the computer locates the data block in the heap through a pointer to the variable and Operates.

In javascript, number, string, boolean, null, and undefined type data are value Types. Since the space occupied by value type data is fixed, they can be stored in a narrow memory stack Area. This kind of storage is more convenient for the computer to find and operate, so the execution speed is very fast.

For the object type data (including function and array), because their size is not fixed, it can not be stored in the stack area, can only be allocated to the heap area, if stored in the stack area, it will reduce the speed of the computer addressing. The heap space is not fixed, so it is appropriate to store the size of the object data, and then store the address of the heap in the stack area, and the address is fixed, so this method of separating the storage does not affect the Computer's addressing speed, the performance of the variable has no negative impact ().

In the JavaScript language, objects such as object, function, and array are reference-type Data. Many languages treat strings as reference data, not value types, because the length of the string is Variable. But JavaScript is special, and it handles strings as value Types. however, string literals are handled in copy and pass operations in the form of reference Data.

JavaScript data Types-value types and reference types

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.