JavaScript is used for both. In the browser it runs the frontend: it reads and updates the page, handles clicks and calls APIs. On a server it runs the backend through a runtime such as Node.js, Deno or Bun, where it serves APIs, talks to databases and handles authentication.
The language is the same in both places. What changes is the environment around it: the browser gives you the DOM and fetch, while a server runtime gives you the file system, network sockets and process control.
Every browser ships a JavaScript engine (V8 in Chrome and Edge, SpiderMonkey in Firefox, JavaScriptCore in Safari). Outside the browser, runtimes embed an engine and add server APIs. Node.js and Deno use V8, and Bun uses JavaScriptCore.
| Frontend (browser) | Backend (runtime) | |
|---|---|---|
| Typical runtimes | Chrome, Firefox, Safari, Edge | Node.js, Deno, Bun |
| Main APIs | DOM, fetch, Web Storage, Web Workers | node:fs, node:http, streams, child processes |
| Job | Render UI, respond to user input | Serve APIs, query databases, run jobs |
| Security model | Sandboxed, same-origin policy | Full access to the machine unless restricted |
| Globals | window, document | process, Buffer (Node.js) |
Code that only uses the language itself (arrays, Map, Promise, classes, modules) runs unchanged in both. Code that touches document only works in a browser, and code that touches node:fs only works on a server.
Frontend JavaScript runs after the browser parses your HTML. It selects elements, listens for events and changes the page.
The script waits for a click, calls an API with fetch, and writes the result into the page. type="module" enables import and defers execution until the document is parsed. Use textContent rather than innerHTML for untrusted text to avoid cross-site scripting.
Most large frontends use a framework or library such as React, Vue, Angular, Svelte or Solid. They manage state and re-render the UI efficiently. Meta-frameworks such as Next.js, Nuxt and SvelteKit add routing and server rendering on top.
On the server, JavaScript handles HTTP requests. The example below uses only built-in Node.js modules, so it runs with node server.mjs on a current LTS release.
In real projects you normally add a framework such as Express, Fastify or Hono for routing, validation and middleware, plus a database driver or ORM. Node.js uses an event loop, so it handles many concurrent I/O-bound requests well. CPU-heavy work belongs in worker threads or a separate service so it does not block the loop.
Because both sides use the same language, you can share validation rules, types and utilities. TypeScript makes this safer by checking the contract between the two.
The browser can call this for instant feedback, and the server must call it again, because you can never trust input that came from a client. Shared code must avoid environment-specific globals so it runs in both places.
A common setup is a framework that renders on the server and hydrates in the browser, with the same codebase handling pages and API routes. Teams choose this to reduce context switching and to hire across the whole stack.
JavaScript is not the right backend for every job. Heavy numeric computing, machine learning and some systems work often fit Python, Go, Java or Rust better. Many teams run a JavaScript frontend against a backend written in another language, which works fine because the two communicate over HTTP or WebSocket.
Yes. Node.js, Deno and Bun execute JavaScript on your computer or server with no browser involved.
Not to run it, since the browser executes it directly. Most projects still use Node.js to install packages and run build tools such as Vite.
No. They are unrelated languages with similar names. JavaScript is dynamic and runs in browsers and runtimes, while Java is a statically typed language that runs on the JVM.
Start with the language and the browser, because feedback is immediate. Then add Node.js and build a small API so you see both sides.
JavaScript is a frontend language because every browser runs it, and a backend language because runtimes like Node.js, Deno and Bun run it on servers. The language is shared, but the APIs differ: the DOM and fetch in the browser, file, network and process APIs on the server. Share validation and types between both sides, and still validate again on the server.