VS Vikesh Singh

Courses JavaScript Lesson 1

JAVASCRIPT · LESSON 1 OF 18

What JavaScript Actually Does in the Browser

· 4 min read

JavaScript is the one programming language that runs natively inside every web browser.

JavaScript is the one programming language that runs natively inside every web browser. When you load a page, the browser downloads three kinds of files: HTML for structure, CSS for style, and JavaScript for behavior. HTML and CSS are mostly static — they describe what the page looks like. JavaScript is what makes a page actually do things: respond to a click, validate a form, update content without reloading, or fetch fresh data from a server.

How JavaScript Runs In The Browser

Here is the part beginners often miss: JavaScript does not run on its own. It runs inside a host environment, and in a browser that host is called the JavaScript engine (Chrome and Node both use V8, Firefox uses SpiderMonkey, Safari uses JavaScriptCore). The engine reads your code, turns it into something the machine can execute, and gives it access to browser-specific tools like the page itself, timers, and network requests. Node.js later took that same engine and let it run outside the browser, on a server, which is why the same language can power both a webpage and a backend.

A key idea for this whole course: JavaScript in the browser is single-threaded. It can only do one thing at a time on the main thread. This matters because if you write code that takes a long time to finish, the whole page freezes — buttons stop responding, scrolling stutters. Later lessons on asynchronous JavaScript exist specifically to work around this limitation.

Adding JavaScript To A Page

Let's look at the three ways JavaScript gets into a page.

<script>
  console.log("Inline script running");
</script>
<script src="app.js"></script>

The first example writes JavaScript directly inside the HTML file. The second loads it from a separate .js file, which is almost always the better choice once your code grows past a few lines — it keeps your HTML clean and lets the browser cache the file.

Script Placement And The Defer Attribute

A very common beginner mistake: putting a <script> tag in the <head> and trying to interact with page elements that haven't loaded yet.

<head>
  <script>
    const button = document.querySelector("#start");
    button.addEventListener("click", function () {
      console.log("Clicked by Vikesh Singh");
    });
  </script>
</head>
<body>
  <button id="start">Start</button>
</body>

This throws an error, because when the script runs, the browser hasn't parsed the <button> yet, so document.querySelector("#start") returns null. There are two standard fixes: place the script tag at the end of the body, right before the closing </body> tag, or add the defer attribute to a script in the head, which tells the browser to wait until the HTML is fully parsed before running it.

<script src="app.js" defer></script>

defer is the modern, recommended approach for external scripts. It downloads the file in the background while the page keeps loading, then runs it only after the HTML is ready, in the order the scripts appear on the page.

Using Console.log To Debug

One more thing worth knowing early: console.log() is your best friend while learning. It prints a value to the browser's developer console (open it with F12 or right-click → Inspect → Console) so you can see what your code is actually doing, rather than guessing.

console.log("Hello from Iron Man's JavaScript file");
console.log(2 + 2);
console.log(typeof "hello");

A quick tip on file organization that pays off fast: keep one JavaScript file per rough area of behavior once your project grows past a single page (for example, nav.js for navigation, forms.js for form handling), rather than one giant file. The browser can load multiple script tags, each with its own defer attribute, and they still run in the order they appear in the HTML. This keeps any single file small enough to actually read in one sitting, which matters more than it sounds like early on, because the alternative — one 2,000 line file — makes even small bugs hard to track down later.

It also helps to know that the browser parses your HTML from top to bottom, which is exactly why script placement matters so much. If you genuinely need a script to run early, before the rest of the page parses, you can still do that deliberately, but understand the trade-off: it will not be able to touch any element defined further down the page. Most of the time, the safest default is simply placing your script tag right before the closing body tag, or using defer, and only breaking that rule when you have a specific, understood reason to.

To make this stick, try this: create a file called app.js, add a console.log statement, link it to a simple HTML page with the defer attribute, and open the page's developer console to confirm your message shows up. Getting comfortable with this loop — write code, check the console, fix, repeat — is the single most useful habit you can build before anything else in this course.