JS stack concept

  • JS variables are stored in memory, and memory has two areas for variables, namely the stack area and the heap area
  • A stack is like a container, small in capacity and fast in speed
  • Pile like a room, large capacity

Base type value store

  • The value of the basic data type (Undefined, Null, String, Boolean, Number) occupies a fixed amount of space and is stored in stack memory
  • The stack follows lifO: a variable is declared, and multiple assignments take the last value
var num1 = 10;
num1 = 15;
num1 = 20;
console.log(num1);  // 20

var num2 = num1;
console.log(num2===num1); // true

num2 = 30;
console.log(num2===num1);  // false
Copy the code

Reference type value store

  • The value of a reference type (Object) is an Object that is stored in heap memory
var people1 = {
    name: 'Tom',
    age: 20
}
var people2 = people1;
people2.name = 'Davi';
console.log(people1.name);  // "Davi"
Copy the code