JavaScript
A programming language that runs in web browsers and on servers, used to create interactive web pages, applications, and many kinds of software.
Also known as: JS, ECMAScript
JavaScript is a high-level programming language created in 1995 by Brendan Eich at Netscape. It was originally designed to add interactivity to web pages and is now used across the web (in browsers and servers), in mobile apps, in desktop applications, in databases, and in many other environments.
JavaScript is one of the three core technologies of the web alongside HTML (structure) and CSS (presentation). The language is standardized as ECMAScript (ES); the names are used interchangeably in most contexts.
Where JavaScript runs
| Environment | Runtime |
|---|---|
| Web browsers | Built into Chrome (V8), Firefox (SpiderMonkey), Safari (JavaScriptCore) |
| Servers | Node.js (V8), Deno (V8), Bun (JavaScriptCore) |
| Mobile apps | React Native, NativeScript, Capacitor |
| Desktop apps | Electron, Tauri |
| Edge networks | Cloudflare Workers, Vercel Edge, Deno Deploy |
| Databases | MongoDB scripts, PostgreSQL functions (with extensions) |
| Embedded systems | Espruino, Johnny-Five for hardware |
The same language with different runtimes and APIs.
Basic syntax
// Variables
const name = "Jane";
let count = 0;
// Functions
function greet(person) {
return `Hello, ${person}`;
}
// Arrow functions
const double = (n) => n * 2;
// Conditionals
if (count > 0) {
console.log("Positive");
} else {
console.log("Zero or negative");
}
// Arrays
const fruits = ["apple", "banana", "cherry"];
fruits.forEach((fruit) => console.log(fruit));
// Objects
const user = {
name: "Jane",
email: "[email protected]",
greet() {
return `Hello, ${this.name}`;
}
};
// Async / await
async function fetchData(url) {
const response = await fetch(url);
return await response.json();
}
What JavaScript does in the browser
JavaScript in the browser interacts with the page through the Document Object Model (DOM):
- Manipulate page content. Add, remove, or modify HTML elements
- Respond to user events. Click, hover, scroll, keyboard input, form submission
- Make network requests. Fetch data from APIs without reloading the page
- Animate elements. Smooth transitions and complex animations
- Handle forms. Validation, submission, autofill
- Store data. Cookies, localStorage, sessionStorage, IndexedDB
- Use device APIs. Geolocation, camera, microphone, push notifications, vibration
- Render graphics. Canvas, WebGL, WebGPU
- Build single-page applications. Frameworks like React, Vue, Svelte, Angular
JavaScript on the server
Node.js (introduced in 2009) brought JavaScript to server environments. Server-side JavaScript powers:
- Web server applications (Express, Fastify, Hono, Koa)
- API backends
- Build tools (webpack, Vite, esbuild)
- Static site generators (Astro, Eleventy, Next.js)
- Real-time applications (chat, collaboration, dashboards)
- Command-line tools
Deno (2020) and Bun (2023) are newer alternatives to Node.js with different design choices.
ECMAScript versions
JavaScript is standardized as ECMAScript. Major versions:
| Version | Year | Notable additions |
|---|---|---|
| ES5 | 2009 | strict mode, JSON, array methods |
| ES6 / ES2015 | 2015 | let/const, arrow functions, classes, modules, promises, template literals |
| ES2016 onwards | Annual | Incremental additions: async/await, optional chaining, nullish coalescing, etc. |
Most modern JavaScript uses ES2015+ features. Browsers receive new features regularly.
Common frameworks and libraries
For browser-based UI:
| Framework / library | Approach |
|---|---|
| React | Component-based UI; massive ecosystem |
| Vue | Component-based UI; gentler learning curve |
| Svelte | Compiles components to vanilla JS at build time |
| Angular | Full-featured framework from Google |
| Solid | Fine-grained reactivity, similar API to React |
| Lit | Lightweight web components |
| htmx | HTML-driven interactivity, minimal JavaScript |
| Alpine.js | Lightweight reactivity for sprinkles of interactivity |
For state management, routing, animations, data fetching, etc., there are many specialized libraries.
TypeScript
TypeScript is a superset of JavaScript that adds static type checking. It compiles to standard JavaScript and is widely used in larger projects:
function greet(name: string): string {
return `Hello, ${name}`;
}
TypeScript catches many common errors at compile time and provides better tooling (autocomplete, refactoring) than vanilla JavaScript. It is increasingly the default for new projects.
JavaScript and performance
JavaScript performance affects:
- Page load time. Large JS bundles take time to download and execute
- Interactivity. Heavy JS execution blocks the main thread
- Battery and CPU. Particularly on mobile devices
- Bundle size. Even unused code adds to download time without tree-shaking
Common performance practices:
- Use code splitting to load only what’s needed
- Defer non-critical scripts
- Tree-shake unused exports at build time
- Avoid large dependencies for small features
- Measure with browser DevTools Performance tab
How much JavaScript a site needs
The right amount of JavaScript varies by site type:
| Site type | Typical JavaScript footprint |
|---|---|
| Marketing site | Minimal, perhaps 0–50 KB for forms and interactivity |
| Documentation | Moderate, search, navigation, possibly framework |
| Blog | Small to moderate, depends on features |
| Web application | Large, 200 KB to several MB |
| Single-page application | Largest, entire app loads upfront |
Modern frameworks like Astro and Eleventy emphasize shipping minimal JavaScript by default; React-based frameworks (Next.js, Remix) ship more by default.
Common misconceptions
- “JavaScript is the same as Java.” They share part of a name but are unrelated languages with different design and use cases.
- “You need JavaScript to build a website.” Many sites work without JavaScript; static HTML is sufficient for content-focused pages.
- “More JavaScript means a better user experience.” Often the opposite; lighter pages tend to feel faster and more responsive.
- “JavaScript only runs in browsers.” It runs in browsers, servers, mobile apps, desktop apps, edge networks, and many other environments.