Variables & Data Types
How JavaScript stores and manages values. The foundation of everything.
Overview
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
var, let & const
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
- ✓Use
constby default - ✓Use
letonly when you need to reassign - ✗Avoid
var— it has confusing scoping and hoisting
Primitive Data Types
JavaScript defines seven primitive types plus objects.
Number
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" |
Dynamic Typing
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".
✓ Advantages
- • Quick to write
- • Flexible
- • No type declarations
- • Great for prototyping
✗ Disadvantages
- • Runtime type errors
- • Harder to debug
- • No IDE autocompletion hints
- • Unexpected coercion
typeof Operator
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.
Stack vs Heap
Where JavaScript stores your variables in memory.
📦 Stack
Primitives (fixed size)
- • Numbers
- • Strings
- • Booleans
- • undefined
- • null
- • Symbols
- • BigInts
🏗️ Heap
Objects (dynamic size)
- • Objects {}
- • Arrays []
- • Functions
- • Date
- • RegExp
- • Map / Set
// 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 |
Exercises
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);Frequently Asked Questions
Common questions about variables and data types.