When you open a website, the browser downloads the HTML, CSS, and JS files.
HTML creates the structure, CSS styles it, and JavaScript makes it interactive.
The browser first parses the HTML and builds a DOM (Document Object Model) — a tree structure representing the page.
It also builds a CSSOM (CSS Object Model) from CSS.
Then, it combines DOM + CSSOM into a Render Tree to paint pixels on the screen.
The browser has a JavaScript engine (e.g., Chrome has V8, Firefox has SpiderMonkey).
JS code is parsed, compiled (nowadays to machine code for speed), and executed by the engine.
JavaScript runs in a single thread inside the browser.
It uses an event loop to handle tasks:
Call Stack: Executes functions one by one.
Web APIs: Browser provides APIs like setTimeout, fetch, DOM manipulation.
Callback Queue: Stores tasks (e.g., click events, API responses) and feeds them back to the call stack when it's empty.
While JS is single-threaded, the browser handles tasks like AJAX, timers, and user input asynchronously via Web APIs and the event loop. So your page doesn’t freeze.
If JS changes the DOM or styles, the browser may do a reflow (layout calculation) and repaint the page.
Browsers now also use Web Workers for running heavy JS in the background.
JS modules and Promises/async-await help in managing code cleanly and asynchronously.
Copyright ©2025. All Rights Reserved Emb