JavaScript — Fundamentals
Variables & Scope (`let`, `const`, `var`)
A variable is a named box that holds a value. JavaScript gives you three ways to declare one, and the difference between them is genuinely important, not cosmetic.
var is scoped to the nearest function, not the nearest block — a var declared inside an if block "leaks" out to the whole function. This is a real, historical source of bugs.let and const are scoped to the nearest block ({ } — an if, a for loop, a function body). This matches what most people intuitively expect.const doesn't mean "immutable" — it means the variable binding itself can't be reassigned. An object or array stored in a const can still have its contents changed:Default rule: use const unless you know the variable needs to be reassigned, in which case use let. Avoid var in new code entirely.
Functions & Closures
A function is a reusable block of code. JavaScript has several ways to write one:
Closures are one of JavaScript's most powerful — and most interview-tested — features. Think of a closure like a backpack: when a function is created, it packs up references to the variables it needs from its surrounding scope, and it carries that backpack with it wherever it goes, even after the outer function has already finished running.
count is not a global variable — nothing outside makeCounter can reach in and change it directly. That's a real, practical use of closures: private state without a class.
Arrays & Objects
Arrays hold ordered lists; objects hold named key-value pairs.
Arrays come with built-in methods for the most common operations — covered in depth in Intermediate (map, filter, reduce), but the basics:
The DOM & Events
The DOM (Document Object Model) is the browser's live, in-memory representation of the HTML on a page — a tree of objects JavaScript can read and modify. When JavaScript changes the DOM, the browser re-renders the visible page to match, immediately.
addEventListener is how JavaScript "listens" for something happening — a click, a keystroke ("input"), a form submission ("submit") — and runs a function in response. This is the actual mechanism behind every interactive element you've ever used on a web page.
`this` Binding
this refers to a different value depending on how a function is called — not where it's written. This trips up nearly everyone at first.
Practical rule: use a regular function when you need this to reflect the calling object (an object method). Use an arrow function when you want this to stay whatever it was in the surrounding code (very common inside callbacks — an event handler inside a class method, for example, where you want this to stay the class instance).
Async Basics: Callbacks → Promises → `async`/`await`
JavaScript handles anything that takes time (loading data, waiting on a timer) without freezing the page, using three generations of the same underlying idea.
async/await is not a different mechanism from Promises — it's syntax that makes Promise-based code easier to read. await can only be used inside a function marked async.
Try It (2 Minutes)
Open your browser console and paste this in:
Notice sayHello and sayNamaste each "remember" their own greeting value, even though makeGreeting already finished running by the time you call them. That's a closure — the exact same mechanism as the counter example above, just with a string instead of a number.

