HTML structures a page and CSS styles it — JavaScript makes it do things. Clicks, form validation, live updates, animations, entire apps: all JavaScript. It's the only language that runs natively in every browser, and with Node.js it runs on servers too. This chapter covers the fundamentals that every later topic — React, Node, even data work — builds on.
What is JavaScript?
JavaScript is a high-level, dynamically-typed language. "Dynamically typed" means you don't declare a variable's type — a variable can hold a number now and a string later. That flexibility is convenient and, occasionally, a footgun; we'll flag the sharp edges as we go.
Running JavaScript
Two easy ways to run code while you learn:
- Browser console — open DevTools (F12) → Console, and type. Instant feedback.
- In a page — add a
<script>tag, ideally just before</body>:
<body><h1 id="title">Hello</h1><script src="app.js"></script></body>
console.log("Hello, Albero!"); // prints to the console
Variables: let & const
Declare variables with const (can't be reassigned) or let (can). Reach for const by default and only switch to let when you know the value must change. Avoid the old var — its scoping rules cause bugs.
const name = "Aanya"; // fixed referencelet score = 0; // will changescore = score + 10; // fine// const name = "Rohan"; // ✗ TypeError: can't reassign a const
Data types
JavaScript has a small set of primitive types plus objects:
| Type | Example |
|---|---|
string | "hello" |
number | 42 |
boolean | true / false |
null | Intentional "no value" |
undefined | A variable not yet assigned |
object | { } |
array | [1, 2, 3] |
Template literals (backticks) let you embed variables directly with ${…} — far cleaner than string concatenation:
const name = "Aanya";const raise = 40;console.log(`${name} got a ${raise}% raise`); // Aanya got a 40% raise
Operators
Arithmetic (+ - * / %) works as you'd expect. The one rule to burn in: use strict equality ===, never ==. The double-equals version coerces types and produces surprises (0 == "" is true). Strict equality checks value and type:
5 === 5 // true5 === "5" // false (number vs string)5 == "5" // true ← avoid == entirelytrue && false // AND → falsetrue || false // OR → true!true // NOT → false// Modern helpersuser ?? "guest" // ?? → use "guest" if user is null/undefineduser?.profile?.email // ?. → safely read nested props
Control flow
const score = 82;if (score >= 90) grade = "A";else if (score >= 75) grade = "B";else grade = "C";// Loop a fixed number of timesfor (let i = 0; i < 3; i++) {console.log("Attempt", i + 1);}// Loop over items (the modern default)const tools = ["Excel", "SQL", "Python"];for (const tool of tools) {console.log(tool);}
For short conditions, the ternary operator is handy: const label = score >= 40 ? "Pass" : "Fail";.
Practice
In the browser console, create a const for your name and a let for a score. Write an if/else that logs a grade, then a for…of loop over an array of your favourite tools. Use a template literal to print each one in a sentence.
Key takeaways
- 01JavaScript adds behaviour to pages and also runs servers via Node.js.
- 02Default to const; use let only when a value must change; never use var.
- 03Template literals (backticks) embed variables with ${…}.
- 04Always compare with === (strict), never == (it coerces types and surprises you).
- 05Use for…of to loop items; ?? and ?. handle null/undefined cleanly.
