Javascript

1. HTML, CSS, and JS Loading

  • 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.

2. Parsing and Rendering

  • 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.

3. JavaScript Engine

  • 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.

4. Single-Threaded Event Loop

  • 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.

5. Non-blocking Behavior

  • 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.

6. Repainting and Reflow

  • If JS changes the DOM or styles, the browser may do a reflow (layout calculation) and repaint the page.

7. Modern Enhancements

  • 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.

Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now