JavaScript variables can be used to hold values of two types: primitive type values and reference type values. Values of basic types come from the following five basic data types: Undefined, Null, Boolean, Number, and String. Primitive type values and reference type values have the following characteristics:

  • Base-type values take up a fixed amount of memory and are therefore stored in stack memory;
  • Copying a value of a primitive type from one variable to another creates a copy of the value;
  • The value of a reference type is an object, stored in heap memory;
  • A variable that contains a reference value does not actually contain the object itself, but a pointer to the object;
  • Copying a reference type value from one variable to another is actually copying a pointer, so both variables end up pointing to the same object;
  • The Typeof operator can be used to determine what base type a value is, and the instanceof operator can be used to determine what reference type a value is.

All variables (both primitive and reference types) reside in an execution environment (also known as a scope) that determines the lifetime of the variable and which part of the code can access it. Here are a few summary points about the execution environment:

  • Execution environment is divided into global execution environment (also known as global environment) and function execution environment.
  • Each time a new execution environment is entered, a chain of scopes is created to search for variables and functions;
  • The local environment of a function has access not only to variables in the function scope, but also to its containing (parent) environment and even to the whole local environment.

  • The global environment can only access variables and functions defined in the global environment, and cannot directly access any data in the local environment.

  • The execution environment of a variable helps determine when memory should be freed.

JavaScript is a programming language with automatic garbage collection, so developers don’t have to worry about memory allocation and reclamation. JavaScript garbage collection routines can be summarized as follows.

  • Values that leave scope are automatically marked as recyclable and therefore deleted during garbage collection.

  • “Tag sweep” is the prevailing garbage collection algorithm. The idea of this algorithm is to tag values that are not currently in use and then reclaim their memory.

  • Another garbage collection algorithm is “reference counting,” the idea of which is to keep track of how many times all values are referenced. JavaScript engines no longer use this algorithm; However, this algorithm can still cause problems when accessing non-native JavaScript objects (such as DOM elements) in IE.

  • The “reference counting” algorithm can cause problems when there are circular references in your code.

  • Dereferencing variables not only helps eliminate circular references, but also benefits garbage collection. To ensure efficient memory recall, global objects, global object properties, and loop reference variables that are no longer used should be dereferenced in a timely manner.