Variables, Data Types & Scope
⏱ 15 min read
JavaScript has three ways to declare variables: var (old — avoid it), let (reassignable), and const (constant — use this by default).
Always prefer const. Use let when you need to reassign. Never use var in modern code.
The 7 Primitive Types:
number — integers AND decimals (no separate int/float like other languages)
Example: let age = 25; let price = 9.99;
string — text in single, double, or backtick quotes
Example: let name = 'Alice';
boolean — true or false (lowercase)
Example: let isLoggedIn = true;
null — intentional absence of a value (you set this deliberately)
undefined — variable declared but not assigned (JavaScript sets this automatically)
symbol — unique identifier (advanced, rarely used directly)
bigint — very large integers: 9007199254740993n
typeof Operator:
typeof 42 -> "number"
typeof "hello" -> "string"
typeof true -> "boolean"
typeof undefined -> "undefined"
typeof null -> "object" <- famous JS bug! null is NOT an object.
typeof {} -> "object"
typeof [] -> "object" <- arrays are objects too!
Template Literals (backtick strings):
const msg = `Hello, ${name}! You are ${age} years old.`
Supports multi-line strings and embedded expressions — far better than concatenation.
Type Coercion — DANGER:
JavaScript automatically converts types, causing surprising results:
"5" + 3 -> "53" (string concatenation!)
"5" - 3 -> 2 (numeric subtraction)
"5" == 5 -> true (loose equality — converts types)
"5" === 5 -> false (strict equality — no conversion)
ALWAYS use === (triple equals) to compare values. Never use ==.
Always prefer const. Use let when you need to reassign. Never use var in modern code.
The 7 Primitive Types:
number — integers AND decimals (no separate int/float like other languages)
Example: let age = 25; let price = 9.99;
string — text in single, double, or backtick quotes
Example: let name = 'Alice';
boolean — true or false (lowercase)
Example: let isLoggedIn = true;
null — intentional absence of a value (you set this deliberately)
undefined — variable declared but not assigned (JavaScript sets this automatically)
symbol — unique identifier (advanced, rarely used directly)
bigint — very large integers: 9007199254740993n
typeof Operator:
typeof 42 -> "number"
typeof "hello" -> "string"
typeof true -> "boolean"
typeof undefined -> "undefined"
typeof null -> "object" <- famous JS bug! null is NOT an object.
typeof {} -> "object"
typeof [] -> "object" <- arrays are objects too!
Template Literals (backtick strings):
const msg = `Hello, ${name}! You are ${age} years old.`
Supports multi-line strings and embedded expressions — far better than concatenation.
Type Coercion — DANGER:
JavaScript automatically converts types, causing surprising results:
"5" + 3 -> "53" (string concatenation!)
"5" - 3 -> 2 (numeric subtraction)
"5" == 5 -> true (loose equality — converts types)
"5" === 5 -> false (strict equality — no conversion)
ALWAYS use === (triple equals) to compare values. Never use ==.
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress