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

user clickevent firesyour handlerDOM update
one click, end to end

key terms

DOM

The document object model: the page as a tree of live objects your code can read and rewrite.

document.body

node

Anything in that tree — elements, text, comments. Elements are the nodes you style; text nodes hold the words.

querySelector returns one

event

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 ul

how it works

  1. 1
    Find the node

    document.querySelector accepts any CSS selector and returns the first match, or null. Null is where the crashes live.

  2. 2
    Listen for events

    addEventListener('click', handler) runs your function when it happens. The event object tells you exactly what was hit.

  3. 3
    Change something

    Update textContent, classList, or styles. The browser notices and repaints — no manual redraw, ever.

  4. 4
    Delegate 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

1const list = document.querySelector('#todo')
2list.addEventListener('click', (e) => {
one listener on the parent covers every row
3 if (e.target.matches('button')) {
the actual clicked element, bubbled up
4 e.target.closest('li').remove()
climb to the row, then remove it
5 }
6})

side by side

== loose

  • converts types before comparing
  • '1' == 1 is true
  • null == undefined is true
  • surprises in production
VS

=== strict

  • no conversion, ever
  • '1' === 1 is false
  • what you read is what runs
  • the lint-enforced default
🪤 gotcha

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

🎤 interview

What is event delegation?watch ↗

🎤 interview

Double equals or triple equals?watch ↗

🎤 interview

Debounce or throttle?watch ↗

🎤 interview

What runs first: setTimeout 0 or a Promise?watch ↗

📚 tutorial

What is a closure?watch ↗

💎 deep

Edit any website live?watch ↗

💎 deep

Reading custom attributes?watch ↗

🔧 useful

Crashing on undefined again?watch ↗