The Mental Model of Variables
Stop thinking of variables as boxes. Learn the correct mental model of variables as pointers.
The Essentials
- Not a Box: Variables are not containers or boxes that hold values.
- Pointers to Values: Think of variables as contacts in an address book that point to a specific value in memory.
- Evaluating Expressions First: When you assign a variable using an expression, JavaScript evaluates the entire expression first to get a single value, and then points the variable to that value.
If you have learned other programming languages or read older programming books, you might have heard variables described as "boxes" or "containers" that hold values.
While that is a helpful beginner metaphor, it is misleading in JavaScript. If you think of variables as boxes, you will eventually encounter a bug that makes absolutely no sense to you.
Instead, I find it much more accurate to think of variables as pointers or contacts in an address book.
The Address Book Metaphor
Imagine your smartphone's contact list. You have a contact named "Stephen" associated with the phone number 555-5555.
The name "Stephen" is the variable. The phone number 555-5555 is the value.
The name "Stephen" does not contain the phone number; it just points to it. When you tell your phone to "Call Stephen," it looks up the name, finds the number it points to, and dials the number.
Expressions and Variables
Take a look at how this pointer model works when you declare and assign a variable using an expression:
let answerToLife = (4 + 1) * 2 * 4 + 2;When JavaScript runs this line, it does not stuff the entire math equation into a box. Instead, it follows a strict sequence:
- It creates the variable name
answerToLifein its internal address book. - It completely evaluates the expression on the right side of the
=sign. It does the math and resolves the expression down to the single value42. - It draws an arrow (a pointer) from the name
answerToLifeto the value42.
The "Scrub" and "Busta" Problem
To prove why the "box" metaphor fails and the "address book" metaphor succeeds, take a look at a classic JavaScript assignment problem using a pop music reference.
What happens to the value of busta at the end of this code?
let scrub = "guy that thinks he's fly";
let busta = scrub;
scrub = "guy that can't get no love";If you think of variables as boxes, you might think:
- I put "fly" in the
scrubbox. - I put the
scrubbox inside thebustabox. - I changed the
scrubbox to hold "no love". - Therefore,
bustamust now be "no love".
But if you run this in your console, you will find that busta is still "guy that thinks he's fly".
I can walk through it with the address book model:
let scrub = "guy that thinks he's fly";: JavaScript addsscrubto the address book and points it to the string value"guy that thinks he's fly".let busta = scrub;: JavaScript evaluates the right side of the equals sign. It looks upscruband sees it evaluates to"guy that thinks he's fly". So, it addsbustato the address book and points it to that same string value.bustadoes not point toscrub; it points to the value thatscrubevaluated to at that exact moment.scrub = "guy that can't get no love";: JavaScript updates thescrubpointer in the address book to point to a brand new string value.
Because busta was pointing directly to the original string value, it does not care that scrub changed its number. busta is completely unaffected.
In JavaScript, when dealing with primitive values (like strings, numbers, and booleans), variables point to the evaluated value at the time of assignment. They do not point to other variables.
Further Reading and Watching
- FrontendMasters: JavaScript First Steps - The source course for this foundational concept.
Keep reading