JavaScript & the DOM
JS is how a page listens, reacts, and gossips.
▶ the lesson · narrated sketch video · tap for sound
the intro
JavaScript makes pages interactive. The browser exposes the page as the DOM — a live tree of objects — and JavaScript can find nodes, listen for events, and change anything. Master querySelector, addEventListener, and events like click and input, and you can build real behavior.
the sample
listen, react, update
const btn = document.querySelector("#go")
btn.addEventListener("click", () => {
document.querySelector("#out")
.textContent = "clicked!"
})fun fact ✦
JavaScript was created in 10 days in 1995 — and `typeof null === "object"` is still here from that week.
the docs
the deep dive
the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid
When the browser parses your HTML, it builds a live tree of objects — and JavaScript is the only language holding the remote control. Find a node, listen to it, rewrite it, and the screen repaints. Every framework you will ever learn is, underneath, a fancier way of doing these three moves.
the DOM is the page; JS is the remote control
key terms
DOM
The document object model: the page as a tree of live objects your code can read and rewrite.
document.bodynode
Anything in that tree — elements, text, comments. Elements are the nodes you style; text nodes hold the words.
querySelector returns oneevent
Something that happened: click, input, submit, keydown. Handlers receive an event object describing it.
addEventListener('click', fn)bubbling
Events rise from the target up through its ancestors, which is why one parent listener can catch its children.
click on li, heard by ulhow it works
- 1Find the node
document.querySelector accepts any CSS selector and returns the first match, or null. Null is where the crashes live.
- 2Listen for events
addEventListener('click', handler) runs your function when it happens. The event object tells you exactly what was hit.
- 3Change something
Update textContent, classList, or styles. The browser notices and repaints — no manual redraw, ever.
- 4Delegate when lists grow
Attach one listener to the parent and read e.target. Children added later are covered with zero extra code.
the code, explained
delegation: one listener, infinite buttons
side by side
== loose
- converts types before comparing
- '1' == 1 is true
- null == undefined is true
- surprises in production
=== strict
- no conversion, ever
- '1' === 1 is false
- what you read is what runs
- the lint-enforced default
Put your script in the head without defer and it runs before the body exists — querySelector finds nothing and you get "cannot read properties of null". Add defer, put the script at the end of body, or wait for DOMContentLoaded. Same code, suddenly works.
quick answers
Should I learn vanilla JS before React?
Yes. React is conventions on top of closures, arrays, and promises. Frameworks go faster when the language underneath is not a mystery.
How do I see the DOM?
The DevTools Elements tab is the live tree — it shows the DOM after browser repairs, which is not always what you wrote.
Is jQuery still a thing?
As a legacy skill, yes; as a choice for new work, no. querySelector and classList cover what jQuery was invented for.
quick hits about JavaScript & the DOM
short answers · interviews, tips, trivia & hidden gems
What is event delegation?watch ↗
Double equals or triple equals?watch ↗
Debounce or throttle?watch ↗
What runs first: setTimeout 0 or a Promise?watch ↗
What is a closure?watch ↗
Edit any website live?watch ↗
Reading custom attributes?watch ↗
Crashing on undefined again?watch ↗