JavaScript Fundamentals: Values, Types & Control Flow
Start from zero: how JavaScript stores values, the quirks of its type system, and how to steer programs with conditionals and loops. The foundation everything else builds on.
4 sections · ~25 min · 5-question quiz (pass ≥ 70%)
1Values and Variables
JavaScript programs are, at their core, values flowing through operations. You store values in variables declared with let, const, or (in legacy code) var.
const name = "Ada"; // can never be reassigned
let score = 42; // can be reassigned
score = 43; // fine
// name = "Grace"; // TypeError: Assignment to constant variable
Rules of thumb:
- Default to
const. Reach forletonly when you genuinely need reassignment. - Avoid
var— it ignores block scope and hoists in confusing ways. constprevents reassignment, not mutation:const arr = [1]; arr.push(2)is legal because the binding still points at the same array.
2The Type System: Primitives vs Objects
JavaScript has exactly seven primitive types: string, number, bigint, boolean, undefined, symbol, and null. Everything else — arrays, functions, dates, plain objects — is an object.
Primitives are compared by value; objects are compared by reference:
"a" === "a"; // true — same value
[1, 2] === [1, 2]; // false! different objects in memory
const a = [1, 2];
const b = a;
b === a; // true — same reference
Two famous quirks worth knowing on day one:
typeof nullreturns"object"— a 30-year-old bug kept for compatibility.NaN === NaNisfalse. UseNumber.isNaN(x)to test for it.
Type coercion. JavaScript converts types implicitly with ==, so "5" == 5 is true. Always prefer === (strict equality), which never coerces.
3Conditionals and Loops
Control flow steers which code runs:
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else {
grade = "C";
}
// Ternary for small decisions:
const label = isAdmin ? "Admin" : "Member";
Truthiness. In a boolean context, these six values are falsy: false, 0, "", null, undefined, and NaN. Everything else is truthy — including "0", [], and {}.
Loops:
for (let i = 0; i < 3; i++) { /* classic counter */ }
for (const item of ["a", "b"]) { /* values of an iterable */ }
for (const key in { x: 1 }) { /* keys of an object */ }
while (queue.length > 0) { queue.pop(); }
Prefer for...of for arrays — for...in iterates keys (as strings!) and can surprise you.
4Arrays and Objects: Your Everyday Data Structures
Arrays are ordered lists with a rich method set. The three you'll use daily:
const nums = [1, 2, 3, 4];
nums.map(n => n * 2); // [2, 4, 6, 8] — transform each item
nums.filter(n => n % 2 === 0); // [2, 4] — keep matching items
nums.reduce((sum, n) => sum + n, 0); // 10 — fold into one value
None of these mutate the original array — they return new ones.
Objects are key–value maps:
const user = { name: "Ada", role: "admin" };
user.name; // "Ada" (dot access)
user["role"]; // "admin" (bracket access — works with dynamic keys)
const { name, role } = user; // destructuring
const updated = { ...user, role: "owner" }; // spread: shallow copy + override
Destructuring and spread are everywhere in modern code — make them muscle memory.