To write clean, bug-free JavaScript, you need a solid mental model of what happens behind the scenes when a browser downloads, parses, and executes your code.
Every modern web browser contains two primary components for web pages:
+-----------------------------------------------------------------------------+ | JAVASCRIPT RUNTIME ENVIRONMENT | +-----------------------------------------------------------------------------+ | [ Call Stack ] [ Memory Heap ] | | Tracks function execution Stores variables, objects, & | | frames (LIFO) allocated memory data | +-----------------------------------------------------------------------------+ | [ Web APIs ] (Provided by the Browser, NOT JavaScript itself!) | | DOM Events, setTimeout(), fetch(), LocalStorage, console.log | +-----------------------------------------------------------------------------+ | [ Callback Queue ] [ Event Loop ] | | Holds finished async tasks Continuously checks if Call Stack is | | ready to execute empty; pushes callbacks to Stack! | +-----------------------------------------------------------------------------+
JavaScript is a single-threaded programming language, meaning it has only one call stack and can execute only one task at a time.
An unstructured pool of computer RAM where objects, arrays, and functions are allocated and stored. Unused memory is automatically freed by the engine's Garbage Collector.
If JavaScript is single-threaded, how can it fetch data from an API without freezing the entire browser screen?
fetch, setTimeout, DOM click events) are delegated to the browser's Web APIs.There are three ways to embed JavaScript into an HTML page:
Separate your logic into a .js file:
Inside an HTML <script> block:
defer vs asyncWhere should you place <script> tags in your HTML?
defer in the <head>!defer: Downloads JavaScript in the background while HTML parsing continues. Executes scripts only after the full HTML document is parsed, preserving script execution order.async: Downloads in the background, but interrupts HTML parsing and executes immediately as soon as download completes (great for independent analytics scripts like Google Analytics).defer attribute to external <script> tags in <head> considered best practice?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| What is JavaScript? | Setting up Environment: VSCode, Live Server |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.