Hello! Welcome to the first lesson of our second module.
In our last module, we covered the absolute fundamentals of JavaScript, ending with a crucial skill: how to read error messages and debug your code. You learned that errors are not a roadblock, but rather helpful signposts from the JavaScript engine.
Today, we shift from fixing code to understanding how our programs store information. This lesson is all about variables. You've seen them briefly before, but now we're going to dive deep. Our goal is to explain how variables reserve space in computer memory to store data. This is a foundational concept that will demystify much of what happens when you write code, aligning perfectly with your goal to understand why things work the way they do.
What is a Variable? A Look at Computer Memory
At its core, a variable is simply a named label for a location in your computer's memory. Think of your computer's memory as a giant grid of mailboxes, each with a unique address. When you create a variable, you're asking the JavaScript engine to find an empty mailbox, put some data inside it, and stick a label on the front so you can find it again easily.
To get a better visual for this, let's start with a short video that explains this "boxes and addresses" model of computer memory.
Understand computer memory for JavaScript
The video 'Understand computer memory for JavaScript' provides an excellent, beginner-friendly analogy for what computer memory is and how a variable declaration is translated into an action at the memory level.
Please watch from the beginning to 02:49. Focus on these two key ideas: Memory as a series of boxes (bytes), each with an address. What happens when you declare a variable like let num1 = 40; – how it finds a space and stores the value.
As the video showed, writing let num1 = 40; isn't magic. The JavaScript engine performs a real, physical action: it reserves a specific number of memory "boxes" at a specific address and stores the binary representation of the number 40 in them. The variable name num1 becomes our human-readable shortcut to that memory address.
The Memory Life Cycle
This process of using memory for a variable follows a consistent, three-stage cycle, which is managed automatically by the JavaScript engine.
JavaScript's Memory Management Explained
The article 'JavaScript's Memory Management Explained' by Felix Gerschau breaks down this process into a clear 'Memory life cycle'.
Read the short section titled 'Memory life cycle'. You don't need to read beyond that for now. Pay attention to the three stages: Allocate, Use, and Release.
So, the three stages are:
- Allocate Memory: The engine reserves a space in memory when you declare a variable.
- Use Memory: You read from or write to the variable in your code (e.g.,
console.log(num1)ornum1 = 50). - Release Memory: When the memory is no longer needed (for example, when a variable is no longer reachable), the JavaScript engine automatically cleans it up to free the space. This automatic process is called garbage collection.
Two Kinds of Memory: The Stack and The Heap
Now for the most important part of today's lesson. The JavaScript engine doesn't just have one big pool of memory. It organizes it into two main regions, each with a different purpose: the Stack and the Heap. Where your variable's data is stored depends on its data type.
This is a critical "under the hood" concept. Understanding the difference between the Stack and the Heap will be essential for grasping more advanced topics later, especially when we discuss the difference between primitive and reference types.
Let's watch a video that visualizes this distinction.
JavaScript Under The Hood [4] - Memory Storage
In 'JavaScript Under The Hood - Memory Storage', Brad Traversy provides a fantastic visual explanation of the Stack and the Heap, showing what kind of data goes where.
Watch from 02:22 to 05:20. As you watch, focus on: What types of data are stored on the Stack (static, fixed-size primitives). What types of data are stored on the Heap (dynamic, variable-size objects). The diagrams showing how a primitive value is stored directly on the stack, while an object variable on the stack only holds a reference (an address) to the actual object in the heap.
Let's break down those concepts further.
The Stack: Fast & Orderly
The Stack is a highly organized, "last-in, first-out" (LIFO) region of memory. Think of it as a stack of books. You can only add or remove the book at the very top.
- What's stored here? Primitive data types (
string,number,boolean,null,undefined,symbol,bigint). These values have a fixed, known size. It also stores references to objects on the Heap. - Why? Because the size of the data is fixed, the engine can allocate memory for it very quickly. Access is extremely fast. This is called static memory allocation.
The Heap: Large & Unstructured
The Heap is a much larger, less-organized region of memory. Think of it as a big, open warehouse where you can store items of any size.
- What's stored here? Reference data types (
objects,arrays,functions). The size of these can change during the program's execution (e.g., you can add new items to an array). - Why? The Heap is needed for data that doesn't have a fixed size. The engine allocates memory as needed. This is called dynamic memory allocation. Because it's less organized, it's slightly slower to access than the Stack.
This table from the "Mastering JavaScript Memory" article provides a great side-by-side comparison:
| Feature | Stack Memory | Heap Memory |
|---|---|---|
| Data Type | Primitives & References | Non-primitives (objects, arrays, functions) |
| Structure | Fixed-size, LIFO | Dynamic, less structured |
| Speed | Fast | Slower due to dynamic nature |
| Memory Cleanup | Automatic (when variable goes out of scope) | Automatic (via Garbage Collection) |
The Big Picture: How They Work Together
So, how do the Stack and Heap interact? All variables are first created on the Stack.
-
If you declare a primitive value, the value itself is stored directly on the Stack.
let score = 100;// The number 100 is on the stack. -
If you declare a reference value (like an object), the object itself is created in the Heap. A pointer or reference (which is just a memory address) to that object is then placed on the Stack.
let player = { name: 'Alex' };// The{ name: 'Alex' }object is in the heap. A memory address pointing to it is on the stack.
This image beautifully illustrates the entire concept:

Test your understanding!
Imagine you write the following three lines of code. For each variable, state whether its primary data will be stored on the Stack or in the Heap.
const name = 'Maria';
let age = 32;
const userProfile = { id: 123, email: 'maria@example.com' };
Show answer
name: The value'Maria'is a string (a primitive type), so it will be stored directly on the Stack.age: The value32is a number (a primitive type), so it will be stored directly on the Stack.userProfile: The value{...}is an object (a reference type). The object itself will be stored in the Heap. TheuserProfilevariable on the Stack will hold a reference (memory address) pointing to that object in the Heap.
Conclusion
Congratulations on getting through this deep dive into memory! Understanding this "under the hood" mechanism is a significant step toward becoming a proficient developer.
Key Takeaways:
- Variables are named pointers to memory locations.
- The JavaScript engine manages a memory life cycle: Allocate, Use, and Release.
- There are two main memory areas: the Stack and the Heap.
- The Stack is fast and organized, used for storing fixed-size data like primitive types (numbers, strings, etc.) and references.
- The Heap is a larger, dynamic space for storing variable-size data like objects, arrays, and functions.
- When you create an object, the object data lives in the Heap, and your variable on the Stack holds a reference to it.
This distinction between data stored directly on the stack (primitives) and data stored by reference in the heap (objects) is one of the most important concepts in JavaScript.
In our next lesson, we will build directly on this foundation. We will explore how to declare variables using let, const, and var, and discuss their differences. We will then immediately revisit today's topic to understand the profound implications of the Stack vs. Heap split when you assign one variable to another—the difference between "pass-by-value" and "pass-by-reference".