Loading...
Loading...
How JavaScript stores and manages values. The foundation of everything.
Before you can build anything in JavaScript you need the vocabulary the language is built on: what JavaScript is, how to store data, how to combine it, how values change type, and how to talk to the user. Every later module assumes you are fluent in these five ideas.
Named containers that store values so your program can remember and reuse data.
Every JavaScript journey starts with the fundamentals. Before loops, functions, or frameworks, you need to understand what JavaScript is, how to declare variables, what types values can have, and how to print results to the console.
Whether you are brand new to programming or coming from another language, this module gives you a solid foundation. Each chapter includes interactive visualizations so you can watch values move through memory instead of only reading about them.
Every program is just data moving and changing over time — a score going up, a cart filling, a form being typed. Variables are how you capture that data and give it a name you can refer to later. Choosing let vs const correctly is also the first habit that prevents a whole class of bugs.
Programs work by processing data. Variables store it.
Programs work by processing data. Data can be names, numbers, prices, dates, user input, settings, game scores, and much more.
To store and work with data, JavaScript uses variables — named containers that hold values which can be used later in a program.
// Values in JavaScript: "Hello" // string 42 // number true // boolean null // intentional empty
Three ways to declare variables. Only two you should use.
Use for values that will change.
let score = 0; score = 10; // ✓ can reassign score = 25; // ✓ works fine // let score = 5; // ✗ Error: already declared
Best Practice
const by defaultlet only when you need to reassignvar — it has confusing scoping and hoistingJavaScript defines seven primitive types plus objects.
Integers and floating-point values. JavaScript uses a single Number type for both.
let age = 25; let price = 9.99; let negative = -20; let infinity = Infinity; let notANumber = NaN; console.log(typeof age);
💡 JavaScript numbers follow IEEE 754 double-precision format (64-bit).
| Type | Example | typeof |
|---|---|---|
| Number | 42 | "number" |
| String | "hello" | "string" |
| Boolean | true | "boolean" |
| Undefined | undefined | "undefined" |
| Null | null | "object" ⚠️ |
| Symbol | Symbol() | "symbol" |
| BigInt | 123n | "bigint" |
Variables can change types at any time.
JavaScript is a dynamically typed language. This means variables can hold different types of values without explicitly declaring their type.
The same variable can store a number, then a string, then a boolean — JavaScript won't complain.
let value = 10; console.log(typeof value);
We start by storing a number. typeof confirms it's a "number".
Check what type a value is at runtime.
The typeof operator returns a string indicating the type of a value. It's a useful debugging tool.
typeof 42 // "number" typeof "hello" // "string" typeof true // "boolean" typeof undefined // "undefined" typeof null // "object" ← bug! typeof Symbol() // "symbol" typeof 123n // "bigint" typeof {} // "object" typeof [] // "object" typeof function(){} // "function"
Try it — typeof checker
💡 Gotchas:typeof null returns "object" (historical bug), and typeof [] also returns "object". Use Array.isArray() to check for arrays.
Where JavaScript stores your variables in memory.
Primitives (fixed size)
Objects (dynamic size)
// Primitives → copied by value let a = 10; let b = a; // b gets its own copy b = 20; console.log(a); // still 10 // Objects → copied by reference let obj1 = { x: 1 }; let obj2 = obj1; // both point to same object obj2.x = 99; console.log(obj1.x); // 99!
| Stack | Heap |
|---|---|
| Fixed size | Dynamic size |
| Fast access | Slower access |
| Copied by value | Copied by reference |
| Auto-cleaned (scope) | Garbage collected |
Test your understanding.
What keyword should you use to declare a variable that won't change?
What does this output?
let x = 5;
x = "hello";
typeof x;What is the typeof null?
What does this output?
let a;
console.log(a);Common questions about variables & types.
Step through the code and watch variables change
1let message = "Hello";2let count = 0;34count = count + 1;5message = "World";67console.log(message, count);
No output yetJavaScript creates a variable called "message" and stores the string "Hello" in memory.
Test your understanding — 3 questions
What will this code output?
let x = 5;
x = 10;
console.log(x);