JavaScript data types -- value types and reference types, what data types are returned by typeof of javascript

Value Type: Also known as primitive data or primitive value.

 

This type of value is stored in the stack. The stack is a special data structure in memory, also known as a linear table. The stack stores data according to the principle of last-in, first-out. The data that enters first is pushed to the bottom of the stack, and finally inserted. The (push) data is placed on the top of the stack. When data needs to be read, the data is popped (pop) from the top of the stack, that is, the last data is read out first. Therefore, value types are simple data segments. The location of the variable and the location of the variable value overlap, that is, the data of the value type is stored in the location where the variable is accessed.

 

Reference type: This type of value is stored in the heap, which is a dynamic area in memory that is equivalent to reserved space and is dynamically allocated to code and the stack during program execution.

 

Objects are generally stored in the heap, and then passed to the variables on the stack through a number. This number is the so-called reference pointer (point), so that the variable and the variable value are separated, and they are linked through the pointer. When reading and writing data, the computer finds the data block in the heap through the pointer of the variable and operates it.

 

In JavaScript, number, string, boolean, null, and undefined are all value types. Since the space occupied by value type data is fixed, they can be stored in a narrow memory stack area. This storage method is more convenient for the computer to find and operate, so the execution speed will be very fast.

 

For object type data (including function and array), because their size is not fixed, they cannot be stored in the stack area, but can only be allocated to the heap area. If they are stored in the stack area, the computer addressing will be reduced. speed. The space of the heap is not fixed, so it is very suitable for storing object data of variable size, and then storing the address of the object in the heap area in the stack area, and the size of the address is fixed, so this method of separate storage is not It affects the addressing speed of the computer and has no negative impact on the performance of variables (see figure).

In the JavaScript language, objects such as object, function, and array are all reference data. Many languages ​​treat strings as reference data rather than value types because strings are variable in length. But JavaScript is special in that it treats strings as value types. However, strings are handled as reference data in copy and pass operations.

1. Return data type

  undefined

  string

  boolean

  number

  symbol(ES6)

  Object

  Function

1. Return data type

  undefined

  string

  boolean

  number

  symbol(ES6)

  Object

  Function

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325857025&siteId=291194637