Key Ideas
1What is a Variable?. A variable is a named container that stores a value in memory. Think of it like a labeled box — you put something inside and refer to it later by the label. Variables ...
2var, let, and const
var (Old — Avoid)
var is function-scoped, which means it ignores block boundaries like if statements and loops. It can also be redeclared, and it gets "hoisted" — moved to the top of its scope automatically, which causes surprising behavior.
var-problems.js — Why var causes bugs
JAVASCRIPT
Copy
var name = "Alice";
var name = "Bob"; // No error! Silently redeclared
console.log(name); // "Bob"
var x = 1;
if (true) {
var x = 2; // Same variable! var ignores block scope
}
console.log(x); // 2 (surprising! expected 1)
⚠️
var is function-scoped — causes confusing bugs
Because var doesn't respect block scope, a variable declared inside an if block leaks out to the surrounding function. This causes hard-to-find bugs. Modern code should always use let and const instead.
let (Modern — Use This)
let is block-scoped — it lives only inside the {} where it was declared. It can be reassigned but not redeclared. Use let when you know the value will change over time.
let-examples.js
JAVASCRIPT
Copy
let score = 0;
score = 10; // OK: reassignment is allowed
score = 25; // OK: reassignment again
// let score = 20; // ERROR: can't redeclare with let
if (true) {
let inner = "only inside this block";
console.log(inner); // works fine here
}
// console.log(inner); // ERROR: inner is not defined outside the block
console.log(score); // 25 — still accessible here
const (Constant — Use When Value Won't Change)
const is also block-scoped like let, but it cannot be reassigned after declaration. Use const by default — only switch to let when you know you need to reassign.
const-examples.js
JAVASCRIPT
Copy
const MAX_SCORE = 100;
// MAX_SCORE = 200; // ERROR: Assignment to constant variable
const userName = "Ahmed";
// userName = "Sara"; // ERROR: can't reassign const
// IMPORTANT: const with objects/arrays — the reference is constant,
// but the contents can still be mutated!
const user = { name: "Ahmed", age: 25 };
user.age = 26; // OK! Mutating the object's property
console.log(user.age); // 26
const colors = ["red", "blue"];
colors.push("green"); // OK! Mutating the array
console.log(colors); // ["red", "blue", "green"]
When to Use Which?
Keyword
Scope
Reassignable
Redeclarable
Recommendation
const
Block
No
No
Use by default
let
Block
Yes
No
When value changes
var
Function
Yes
Yes
Avoid — legacy
Section 3
The 7 Primitive Data Types. JavaScript has 7 primitive data types. These are the building blocks of all data — every value you work with is either one of these primitives, or an object built from...
3The typeof Operator. The typeof operator returns a string describing the type of any value. It's extremely useful for debugging and type-checking in JavaScript.
4Type Coercion — JS's Quirky Behavior. JavaScript automatically converts types when you mix them in operations — this is called implicit type coercion. It's one of the most confusing aspects of JavaScript f...
5null vs undefined. Both null and undefined represent "no value" — but they have very different meanings and origins. Knowing the difference prevents common bugs.