What are the data types in JavaScript and how are they different?

There are eight data types in JavaScript, namely Undefined, Null, Boolean, Number, String, Object, Symbol, and BigInt.

Among them, Symbol and BigInt are new data types in ES6:

  • Symbol represents a unique and immutable data type after creation, which is mainly to solve the problem of possible global variable conflicts.
  • BigInt is a digital type of data that can represent integers in arbitrary precision format. BigInt can be used to safely store and manipulate large integers, even if the number exceeds the
  • The range of safe integers that Number can represent.

These data can be divided into primitive data types and reference data types:

  • Stack: primitive data types (Undefined, Null, Boolean, Number, String)
  • Heap: Reference Data Types (Objects, Arrays, and Functions)

The difference between the two types is the difference in storage location:

  • The primitive data type is a simple data segment directly stored in the stack (stack), which occupies a small space and has a fixed size. It belongs to frequently used data, so it is stored in the stack;
  • Objects of reference data types stored in the heap occupy a large space and are not fixed in size. If it is stored in the stack, it will affect the performance of the program running; the reference data type stores a pointer in the stack, which points to the starting address of the entity in the heap. When the interpreter looks for a reference value, it first retrieves its address on the stack, and then gets the entity from the heap after getting the address.

The concept of heap and stack exists in data structures and operating system memory, in data structures:

  • In the data structure, the access method of the data in the stack is first in last out.
  • The heap is a priority queue, which is sorted according to priority, and priority can be specified according to size.

In the operating system, memory is divided into stack area and heap area:

  • The memory in the stack area is automatically allocated and released by the compiler, and stores function parameter values, local variable values, etc. It operates like a stack in a data structure.
  • The heap memory is generally allocated and released by the developer. If the developer does not release it, it may be recovered by the garbage collection mechanism at the end of the program.

Guess you like

Origin blog.csdn.net/weixin_45753871/article/details/127322835