Accent colour
 
Shortcuts: press ?
 

JavaScript Explained — The Language Behind the Interactive Web

Muhammad Tahir November 27, 2026

Introduction

Open almost any website and click something. A menu slides open. A form tells you the password is too short before you press submit. None of that happens in HTML alone. Behind nearly every interactive thing on the web sits a programming language that was written in about ten days in 1995 and was never supposed to become this important.

That language is JavaScript. It runs on 98.9 per cent of websites, and in the 2025 Stack Overflow Developer Survey it was the most used programming language, reported by 66 per cent of developers. It started as a small scripting language for making web pages react to clicks. It now powers servers, mobile apps, and desktop software.

This article explains what JavaScript is, how it works, and where beginners usually go wrong.

What JavaScript Actually Is

JavaScript is a programming language: a way of writing instructions a computer can follow. HTML describes what is on a page. CSS describes how it looks. JavaScript describes what it does. HTML builds the structure, CSS paints it, and JavaScript makes it respond.

A simple example makes the split clear. A button on a page is HTML. Its colour, size, and rounded corners are CSS. What happens when you click it — a hidden panel opening, a number going up, a message appearing — is JavaScript. Without JavaScript, a page is a document you read and scroll. With it, the page responds.

JavaScript is a high-level language, which means you write in words and symbols that read almost like English, and the machine handles the details of memory and hardware. It is dynamically typed: you do not declare what kind of value a variable holds, and the same variable can hold different kinds at different times. That makes the language quick to write — and the source of some of its most famous mistakes.

Functions are first-class citizens in JavaScript, a phrase that sounds academic but has a practical meaning: a function can be stored in a variable, passed into another function, and returned from a function, just like a number or a piece of text. This is why JavaScript code often reads as small functions handed around to each other.

The language is defined by a standard called ECMAScript, maintained by a committee known as TC39 at Ecma International. The browsers and other runtimes implement that standard. ES6, or ES2015, was a large update released in 2015; new editions have arrived every year since.

A Short, Messy History

JavaScript was created by Brendan Eich at Netscape in 1995. Pages were static documents; if you wanted anything to happen without loading a new one, you were out of luck. Netscape wanted a small language web designers could use to add behaviour to pages.

Eich built the first version in about ten days. It shipped in a Netscape Navigator beta in September 1995 as LiveScript, and was renamed JavaScript for the official release that December. The name was a marketing decision, made while Java was the most talked-about language in the industry. JavaScript has almost nothing to do with Java beyond a similar-looking syntax.

Microsoft soon shipped its own version, JScript, in Internet Explorer. For several years the two drifted apart, and developers had to write code that worked in both. Netscape submitted the language to Ecma International in November 1996, and the first ECMAScript specification appeared in June 1997.

ECMAScript 3 arrived in 1999, and the next big update did not come until 2009. In 2005, Google Maps showed what a page could do if JavaScript fetched data in the background instead of reloading. In 2009, Node.js took JavaScript out of the browser and onto the server. In 2015, ES6 arrived with classes, modules, arrow functions, and promises.

One detail from 2025 is worth noting. TypeScript, a version of JavaScript that adds static types and is compiled down to plain JavaScript, overtook JavaScript itself as the most used language on GitHub in August 2025. JavaScript is still the foundation — TypeScript is JavaScript with extra rules on top.

How It Runs in the Browser

When you open a web page, the browser downloads the HTML, builds the page, and runs any JavaScript it finds, usually from files linked with a <script> tag. The code runs inside a JavaScript engine built into the browser — V8 in Chrome and Edge, SpiderMonkey in Firefox, JavaScriptCore in Safari — which turns it into machine instructions and runs them.

Modern engines use just-in-time compilation. Instead of interpreting code line by line, the engine watches how it behaves and compiles the parts that run often into fast machine code. That is why a language once dismissed as a toy can now run complex applications smoothly.

JavaScript in the browser runs on a single thread: it executes one thing at a time, in order. A long-running piece of code blocks everything else until it finishes: the page freezes, buttons do not respond, animations stop. This is why the language has an elaborate system for handling work that takes time, covered in the section on asynchronous code.

The browser also gives JavaScript tools that are not part of the language itself. The ECMAScript standard defines the core — numbers, strings, arrays, functions — but says nothing about web pages or networks. Those come from web APIs: the DOM for reading and changing the page, fetch for requesting data, localStorage for saving data, and more. These APIs feel like part of JavaScript, but they belong to the browser; the same code running in Node.js gets a different set of tools.

The DOM: JavaScript's Handle on the Page

When a browser loads a page, it parses the HTML and builds an internal model called the Document Object Model, or DOM — a family tree where the page is the trunk, each element a branch, and the text a leaf. Every heading, paragraph, image, and button becomes an object in that tree, which JavaScript can read and change.

This is how interactivity works in practice. A script finds the button the user clicked, finds the panel that should open, and changes its style or content. The browser redraws the page to match. The HTML file itself does not change — the DOM is a live copy, rebuilt from scratch when the page reloads.

document.querySelector finds an element by a CSS selector, the same kind you use in a stylesheet. element.textContent reads or changes the text inside an element. element.classList.add adds a CSS class, the usual way to change how something looks. document.createElement builds a new element, which is how lists and search results get added without reloading.

The DOM is also where performance problems start. Every change to the DOM can trigger the browser to recalculate layout and repaint, and doing that hundreds of times in a loop is slow. The practical rule is simple: change the DOM as little as possible, and build up a chunk of new content before inserting it. This is one reason frameworks exist, as we will see later.

Events: How Pages Listen

A page does not run JavaScript continuously. Most of the time it waits. When something happens — a click, a key press, a form being submitted — the browser fires an event, and any JavaScript that signed up for it runs. This is event-driven programming: a set of small functions, each waiting for its moment.

You take an element, call addEventListener on it, name the event — click is the most common — and hand it a function to run. When the user clicks, the browser calls your function with an object describing the event: which element was clicked, where the mouse was.

Events also travel. When you click a button inside a form inside a page, the click event fires on the button first, then bubbles up through each parent element in turn. Bubbling is useful: instead of attaching a listener to every item in a long list, you attach one to the list itself and check which item was clicked.

Forms are where beginners meet events most often. Submitting a form normally reloads the page, which is rarely what you want. The usual approach is to listen for the submit event, stop the default behaviour, check the values, and send them with fetch instead, so the user gets instant feedback without the page reloading.

Async: Promises and the Event Loop

Every JavaScript developer meets the same puzzle: the language runs one thing at a time, yet a page can fetch data, wait for the reply, and keep responding to clicks. The answer is the event loop.

The picture has four parts. The call stack is where code runs, one function at a time. Web APIs handle the waiting: timers and network requests happen outside the stack, managed by the browser or by Node.js. The task queue holds callbacks from timers and I/O, waiting their turn. And the microtask queue holds promise callbacks, which get priority over the task queue. The event loop moves work from the queues onto the stack whenever the stack is empty.

A promise is JavaScript's way of representing a value that is not ready yet. When you call fetch to get data from a server, it returns a promise immediately — pending at first, later fulfilled with the data or rejected with an error. You attach code with .then, or wait for it with await inside an async function. The async/await syntax, added in 2017, lets you write asynchronous code that reads almost like ordinary sequential code.

The classic demonstration: Log Start, schedule a zero-millisecond timer, schedule a promise callback, then log End. The output is Start, End, the promise callback, and then the timer callback. The timer was scheduled first, but the promise callback runs first, because the event loop drains the entire microtask queue before taking the next task from the task queue. It surprises everyone the first time.

There is one misunderstanding to clear up: await does not block. When execution reaches an await, the function pauses and hands control back, and the event loop keeps running timers, I/O callbacks, and other code. What blocks the loop is long synchronous work — a heavy computation, a tight loop — whether or not it sits inside an async function. The async keyword does not move code to another thread; a genuinely long task belongs in a Web Worker, or a worker thread in Node.js.

Errors need the same care. A promise that rejects with no handler attached is an unhandled rejection, and in Node.js it stops the process by default. Always attach a .catch or wrap await in try...catch, and when you start several promises at once with Promise.all, make sure each one has a handler from the start.

Node.js: JavaScript Leaves the Browser

For its first fourteen years, JavaScript lived almost entirely in the browser, until Ryan Dahl released Node.js in 2009. Node.js takes Google's V8 engine and pairs it with an event loop and low-level tools for files, networks, and operating system services — a runtime that executes JavaScript outside a browser.

Traditional web servers handled each connection with a thread, and threads are expensive: thousands of connections meant thousands of threads. Dahl's insight was that most of what a server does is wait — for a database, a file, a network reply — and JavaScript's event loop was already built for waiting. One thread could juggle thousands of connections instead of blocking on each one.

That design made Node.js a natural fit for web servers and APIs, command-line tools, and front-end build systems. It also gave JavaScript developers a single language across the whole stack: the same language in the browser and on the server, the same data structures, the same habits. It is a large part of why JavaScript became the most used language in the world.

Node.js also brought npm, now the largest collection of reusable code libraries in any language. Need to validate an email address or talk to a database? There is almost certainly a package for it. That abundance is a strength and a risk: a project can end up depending on hundreds of packages written by strangers, so choosing dependencies carefully is part of the job. The Node.js documentation is the place to start for the runtime, and the MDN JavaScript Guide for the language.

Frameworks vs Vanilla JavaScript

Once a page grows past a handful of interactive elements, plain JavaScript — often called vanilla JavaScript — feels repetitive. Updating the page means finding elements, changing them, and keeping track of what changed. Frameworks exist to take over that bookkeeping.

React, Vue, and Angular are the best-known examples. They share a core idea: instead of changing the page directly, you describe what it should look like given the current data, and the framework works out the smallest set of changes needed. When the data changes, the page updates itself, which is why most professional front-end jobs ask for at least one framework.

The trade is real, though. A framework adds size to the page, a build step, and concepts to learn. A simple page with one button does not need React; a few lines of vanilla JavaScript do the job better. A common mistake is to reach for a framework before understanding the language underneath it: frameworks are written in JavaScript, and debugging them without knowing the language is painful. Get comfortable with vanilla JavaScript first — the DOM, events, promises — and then pick up a framework when a project is big enough to need one.

HTML, CSS, and JavaScript: Who Does What

TechnologyJobExampleRuns where
HTMLStructure and contentA heading, a form, an imageBrowser
CSSAppearance and layoutColours, spacing, responsive designBrowser
JavaScriptBehaviour and interactionA menu opening, a live search, form validationBrowser and server
TypeScriptJavaScript with static typesLarge applications that need stricter checkingCompiled to JavaScript

Common Beginner Mistakes

Every JavaScript developer has made these mistakes.

Using == instead of ===. The double-equals operator converts types before comparing, so '5' == 5 is true, which is rarely what anyone wants. Triple-equals compares without converting, so '5' === 5 is false. Use === by default, and treat == as a special tool you reach for deliberately.

Declaring variables with var. The old var keyword has function scope and hoisting behaviour that surprises beginners: a variable declared inside a block is visible outside it, and using a variable before its declaration gives undefined instead of an error. Modern code uses const for values that do not change and let for values that do.

Forgetting that this changes. The value of this inside a function depends on how the function is called, not where it is written. A method called on an object gets that object as this; the same function passed as a callback may get something else entirely, often undefined. Arrow functions keep the this from the surrounding code, which is why they are the default choice for callbacks.

Treating asynchronous code as if it were instant. A beginner writes let data = fetch(url) and tries to use data on the next line, only to find it is a promise, not the result — the data is not there yet. You have to wait for it with await or .then, and any code that needs the result has to live inside that wait. This is the single most common source of confusion in the language.

Blocking the main thread. A loop that processes a huge list, or a function that sorts ten thousand items on every keystroke, freezes it. The fix is usually to do less work — filter the list before rendering it, or wait until the user pauses typing — or to move the work off the main thread.

Ignoring errors from promises. A rejected promise with no handler fails silently in the browser and crashes the process in Node.js. Every promise chain should end with error handling, and every await should sit inside a try...catch unless the caller is handling it.

Copying code without understanding it. JavaScript has an enormous amount of example code online, and pasting a snippet that works without knowing why is tempting. It works until the day it does not, and then the debugging is far harder than learning the concept would have been. Read the snippet, work out what each line does, and then write your own version.

Frequently Asked Questions

Is JavaScript the same as Java?

No. The name was a marketing choice from 1995, when Java was the most talked-about language. JavaScript resembles Java in surface syntax, but the two are different languages: JavaScript is dynamically typed and prototype-based, while Java is statically typed and class-based. Knowing one does not mean knowing the other, though the general programming concepts transfer.

Do I need to learn HTML and CSS before JavaScript?

Yes, at least the basics. JavaScript in the browser exists to work with the page, and the page is HTML and CSS. You need to read a page's structure before you can sensibly change it with code. A few weeks with HTML and CSS first makes JavaScript far easier.

What is the difference between JavaScript and TypeScript?

TypeScript is JavaScript with static types added. You declare the types of your variables and function arguments, and the compiler checks them before the code runs. It catches a whole class of mistakes early, which is why large projects increasingly use it. TypeScript compiles down to plain JavaScript, so the browser still runs JavaScript in the end. Learn JavaScript first; our TypeScript explained article covers the typed version in detail.

Can JavaScript run outside the browser?

Yes. Node.js runs JavaScript on servers and on the command line. The same language also runs inside mobile apps and desktop apps built with tools like Electron. The core language is the same everywhere; what changes is the set of tools the environment provides.

Is JavaScript hard to learn?

It is one of the easier languages to start with and one of the harder ones to master. The basics — variables, functions, changing a page — are approachable within weeks, and you can see your results immediately in a browser. The harder parts are asynchronous code, the event loop, and the this keyword, and they take months of practice to feel natural.

The Bottom Line

JavaScript is the language that makes the web respond: it listens for clicks, fetches data, and updates the page while it waits. It was written in ten days in 1995, reshaped by the 2015 ES6 update, and carried out of the browser by Node.js in 2009. Today it runs on nearly every website and is the most used programming language in the world.

The ideas that matter most are few: the DOM is the page as a tree of objects, events are how the page listens, and the event loop is how one thread juggles work that takes time. Promises and async/await are the tools for that waiting, and the common mistakes are all avoidable once you know they exi

Was this article helpful?

Muhammad Tahir

Author

 

Get new articles by email

 

Comments