Value vs. Reference Variable Assignment

primitive type (i.e., Boolean, Null, Undefined, String, Number, Symbol, and BigInt.)
  • the JavaScript runtime gets to determine whether that primitive is assigned by reference or by value.
  • It doesn't really matter how it's done because primitives can't be mutated
  • However, when the assigned value is an Array, Function, or Object a reference to the array/function/object in memory is assigned.
In the following example 1, var2 is set as equal to var1. Since var1 is a primitive type (String), var2 is set as equal to var1's String value and can be thought of as completely distinct from var1 at this point. Accordingly, reassigning var2 has no effect on var1.
The object { name: 'Jim' } is created in memory The variable var1 is assigned a reference to the created object The variable var2 is set to equal var1... which is a reference to that same object in memory! var2 is mutated, which really means the object var2 is referencing is mutated var1 is pointing to the same object as var2, and therefore we see this mutation when accessing var1