In JavaScript, a variable goes through three stages:
let x; (The engine registers the name).x = 10; (The engine allocates memory).console.log(x); (The engine retrieves the value).JavaScript uses two distinct structures to store data. Understanding the difference is key to mastering Value vs. Reference.
This is a classic interview topic.
JavaScript
let name1 = "Alice";
let name2 = name1; // A copy is made
name2 = "Bob";
console.log(name1); // "Alice" (Original stays same)
console.log(name2); // "Bob"
JavaScript
let user1 = { name: "Alice" };
let user2 = user1; // Both variables point to the SAME address in the Heap
user2.name = "Bob";
console.log(user1.name); // "Bob" (Wait, what?! The original changed!)
Warning: This is why “mutating” objects is dangerous in large apps. Both variables are just different names for the same memory “bucket.”
let vs. const vs. var (The Final Verdict)var Legacyundefined.if or for loops), leading to “Variable Leakage.”let{ }).constconst does not make objects immutable. It just means the “address” in the stack cannot change.The const Object Trap:
JavaScript
const colors = ["red", "blue"];
colors.push("green"); // Works perfectly!
// colors = ["yellow"]; // Error! (Trying to change the address)
How does JS clean up memory? It uses an algorithm called “Mark and Sweep.”
Look at the code below. Without running it, can you guess the output of the final line?
JavaScript
let a = [1, 2, 3];
let b = a;
b.push(4);
let c = [...a]; // This is called "Spreadding"
c.push(5);
console.log(a);
Explanation:
b = a creates a reference. Adding 4 to b also adds it to a.c = [...a] creates a Shallow Copy (a brand new array in the heap). Adding 5 to c does not affect a.a is [1, 2, 3, 4].A variable is a named memory location used to store data that can change during program execution.
Example (conceptual):
age = 21salary = 50000Here, age and salary are variable names, and values are stored in memory.
Common rules across languages:
_)int, class, if)Age ≠ age)@, #, $)totalMarks instead of tmReserves memory for a variable.
Example:
int count;float price;Assigning a value to a variable.
Example:
int count = 10;float price = 99.99;int x = 5;Example:
function sum() {
int a = 10;
}
A constant is a variable whose value cannot be changed once assigned.
Example:
const int MAX = 100;Fixed values written directly in code:
103.14'A'"Hello"true, falseDefined using keywords or macros:
constfinal (Java)#define (C/C++)Group of named constants.
Example:
enum Days { MON, TUE, WED }
The process of allocating, using, and freeing memory during program execution.
Goals:
| Memory Area | Purpose |
|---|---|
| Stack | Local variables, function calls |
| Heap | Dynamic memory allocation |
| Data Segment | Global & static variables |
| Code Segment | Program instructions |
malloc, calloc, free (C)new, delete (C++)new keyword (Java – handled by JVM)Occurs when memory is allocated but not released after use.
allocate memory
forget to free memory
Automatic memory management system that:
Where a variable is accessible.
Types:
How long a variable exists in memory.
| Variable Type | Lifetime |
|---|---|
| Local | During function execution |
| Static | Entire program |
| Global | Entire program |
| Object | Until destroyed or garbage collected |