JavaScript variables, scopes, and memory

1. Original and reference values

ECMAScript variables can contain two different types of data: raw and reference values

Original value (primitive value) : is the most simple data, which includes 6 kinds of original value Undefined in js, Null, Number, Boolean, String, Symbol. The variable that holds the original value is accessed * by value *

Reference value: An object composed of multiple values cannot access the memory directly because JS cannot operate the memory space where the object is located. When operating the object, it actually operates the reference of the object rather than the actual object itself. Thus, the variable holding the reference value is accessed * by reference *

Dynamic properties:

For reference values, attributes and methods can be added or modified at any time

The original value has no attributes

Copy value:

When the original value is assigned to another variable, the original value is copied to the location of the new variable

When a reference value is assigned from one variable to another, the value stored in the variable is copied to the location of the new variable. The problem with this is that the copied value is actually a pointer to an object stored in heap memory, as shown below

2. Memory management

Improve performance with const and let declarations

Because both const and let are scoped by blocks (not functions), using these two new keywords might get the garbage collector involved sooner than using var

Hide classes and delete operations

A memory leak

1. Declare variables without any keywords

2. Closure references external variables

Static allocation with object pooling