• Home
  • blog
  • Quick Tip: How JavaScript References Work

Quick Tip: How JavaScript References Work

This article was originally published on Medium.

TL;DR: There are NO pointers in JavaScript and references work differently from what we would normally see in most other popular programming languages. In JavaScript, it’s just NOT possible to have a reference from one variable to another variable. And, only compound values (e.g.. Object or Array) can be assigned by reference.

Morpheus: What if I told you, you can write references in JavaScript?

The following terms are used throughout the article:

  • scalar – a singe value or unit of data (e.g. integer, boolean, string)
  • compound – comprised of multiple values (e.g. array, object, set)
  • primitive – a direct value, as opposed to a reference to something that contains the real value.

JavaScript’s scalar types are primitives, but some languages, such as Ruby, have scalar reference types. Note that in JavaScript, the scalar primitive values are immutable while compound values are mutable.

Bottom Line:

  1. The typeof value assigned to a variable decides whether the value is stored with assign-by-value or assign-by-reference.
  2. On variable assignment, the scalar primitive values (Number, String, Boolean, undefined, null, Symbol) are assigned-by-value and compound values are assigned-by-reference.
  3. The references in JavaScript only point at contained values and NOT at other variables, or references.
  4. In JavaScript, scalar primitive values are immutable and compound values are mutable.

Quick Example of Assign-by-Value:

In the code snippet below, we are assigning a scalar primitive value (a number) to a variable and thus assign-by-value applies here. Firstly, the variable batman is initialized and when the variable superman is assigned with the value stored in batman, it creates a new copy of the value and stores it. When the variable superman is modified, batman is left unaffected, as they point to distinct values.

var batman = 7;
var superman = batman;   //assign-by-value
console.log(batman);     //7
console.log(superman);   //8

Assign-by-value example

Quick Example of Assign-by-Reference:

In the code snippet below, we are assigning a compound value (an array) to a variable and thus assign-by-reference applies here. The variables flash and quicksilver are references to the same value (aka shared value). The references will point to the updated value when the shared value is modified .

var flash = [8,8,8];
var quicksilver = flash;   //assign-by-reference
console.log(flash);        //[8,8,8,0]
console.log(quicksilver);  //[8,8,8,0]

Assign-by-reference example

How to Create a New Reference

When the compound value in a variable is reassigned, a new reference is created. In JavaScript, unlike in most other popular programming languages, the references are pointers to values stored in variables and NOT pointers to other variables, or references.

var firestorm = [3,6,3];
var atom = firestorm;   //assign-by-reference
console.log(firestorm); //[3,6,3]
console.log(atom);      //[3,6,3]
atom = [9,0,9];         //value is reassigned (create new reference)
console.log(firestorm); //[3,6,3]
console.log(atom);      //[9,0,9]

Continue reading %Quick Tip: How JavaScript References Work%