1. Why is there stack memory and heap memory in JS?

Usually associated with the JS memory reclamation mechanism, in order to make the program run the memory occupied by the minimum. Use the tool http:// latentflip.com/loupe to help you understand how it works and how JavaScript call stacks/event loops/callback queues interact with each other.

2. What are the types of variables in JS?

1. Basic types: number, String, NULL (special), undefined, Boolean, 2. Reference types :(object) => Array, function, data, RegExp

2.1. The stack memory

It is mainly used to store Pointers to basic types and object variables, giving people a feeling like a linear array of space, each small unit is basically the same size, stored variables are generally known size or known upper limit range, is a simple storage. Stack memory automatically allocates a relatively fixed size of memory space and is automatically released by the system.

2.2. The heap memory

It is mainly used to store reference types such as object, and store object type data whose size is unknown. This is probably why null is stored in stack memory as a variable of type Object.

Heap memory is dynamically allocated memory that varies in size and is not automatically freed.

3. Js data test

3.1. Basic type data testing

var a = 123 // is a primitive type that creates a variable called a (room) at the bottom of the stack to hold 123.
Copy the code

This line of code does the following in memory:

var a = 123 
var b = a // create room B and copy the value of a into a variable called b. [Copy variable value]
Copy the code

123 in A is completely independent of 123 in B, and this value is just a copy of 123 in A. Thereafter, the two variables can participate in any operation without affecting each other.

var a = 123 
var b = a
a = 124 // Create a new room (a) and add the value 124 to it. This will delete the room name of the previous room (A), but will not delete the data in the room. The values of A and B do not affect each other.
Copy the code



Conclusion: Because the original value does not change, only the room number is changed, and the value in the room is not deleted. So the data on the stack stays at the bottom. Just like a camera, when we delete a photo, we delete the name of the photo, but we still have data in the actual memory, so if we want to destroy the data, we have to store a lot of photos internally, overwriting the previous data

3.2. Reference type data testing

var arr = [1.2] // the arr room will be named in stack memory, but if the value is a reference type, the arr room will be placed in heap memory, and the arr room will be stored in [1,2] room name.
Copy the code

var arr = [1.2]
var arr1 = arr // create a room name in the stack named arr1, copy the value of arr into the room of arr1, but copy a reference address, so their values are referred to the heap memory [1,2]
Copy the code

var arr = [1.2]
var arr1 = arr
arr.push(3) // If new data is added to the array, the value of arr1 will change accordingly.
Copy the code




3.3. The const keyword

1. We know that the const keyword is used to define a constant. What const actually guarantees is not that the value of the variable cannot be changed, but that the memory address to which the variable refers cannot be changed. 2. For data of simple types (values, strings, booleans), the value is stored at the stack memory address to which the variable points, and therefore is equivalent to a constant. 3. For composite types of data (objects and arrays), a const pointer to the stack memory address is guaranteed to be fixed, and the data structure it points to is mutable. 4. For simple types, const guarantees that the data held in stack memory cannot be changed. 5. In the case of compound types, const guarantees that the pointer held in stack memory cannot be changed.