The browser downloads HTML, builds the DOM, fetches scripts, then executes JavaScript on the main thread (unless you use Web Workers).
Parse → compile → run
- HTML parser builds DOM tree
- Script tags trigger download and execution
- Engine compiles to bytecode/machine code (JIT)
- Your code can read/mutate DOM and call Web APIs
defer and async
<script defer> runs after HTML parse. async downloads in parallel and runs when ready—order vs blocking matters for bugs.
Important interview questions and answers
- Q: What is the DOM?
A: Live tree representation of the document scripts can change. - Q: Why block the main thread?
A: Long loops freeze UI—use async patterns or workers.
Self-check
- What happens before your script can query #app?
- When use defer on scripts?
Tip: Re-run the playground code for how-js-runs and tweak one line before the MCQs.
Interview prep
- What is the DOM?
Live document tree the browser builds from HTML.