Running JavaScript
JavaScript is unusually flexible about where it runs. The same language powers interactive web pages in the browser, command-line tools and servers under Node.js, and quick experiments in online sandboxes. Knowing the three core ways to execute JS — the browser console, a <script> in HTML, and a file run with Node — lets you pick the fastest path for whatever you are building or learning. This page walks through each, with the exact commands and output you should expect.
The browser console
Every modern browser ships with DevTools, and inside it lives a JavaScript console with a live interpreter. It is the quickest way to try a snippet — no files, no setup. Open it with F12, or Ctrl+Shift+J (Windows/Linux) / Cmd+Option+J (macOS), then click the Console tab.
Type an expression and press Enter. The console both runs the code and prints the result of the last expression:
const greeting = "Hello from the console";
console.log(greeting);
2 + 2 * 10;
Output:
Hello from the console
22
The console is also where console.log() output and runtime errors surface, which makes it the primary tool for debugging. You can inspect objects interactively — expand them, drill into properties, and even reference the last result with the special $_ variable.
Tip: The console runs in the context of the current page, so it can read and manipulate the live DOM. Try
document.titleordocument.body.style.background = "tomato"on any page to see it react instantly.
Embedding JavaScript in HTML
To make JavaScript part of a web page, you load it with a <script> element. You can write code inline or, preferably, reference an external .js file. Place the tag just before the closing </body> so the HTML has parsed before your script runs, or add the defer attribute to a script in the <head>.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Running JS in HTML</title>
</head>
<body>
<h1 id="title">Loading…</h1>
<script>
const title = document.getElementById("title");
title.textContent = "JavaScript is running!";
console.log("Script executed");
</script>
</body>
</html>
When you open this file in a browser, the heading updates to JavaScript is running!, and the console shows:
Output:
Script executed
For real projects, keep your code in a separate file and use type="module" to opt into ES modules (import / export):
<script type="module" src="app.js"></script>
Note: A module script is always deferred and runs in strict mode automatically, so you do not need the
deferattribute or a"use strict"directive withtype="module".
Running files with Node.js
Node.js is a runtime that executes JavaScript outside the browser, built on the same V8 engine that powers Chrome. It is how you run JS for servers, build tools, scripts, and tests. After installing Node, confirm it is available:
node --version
Output:
v22.11.0
Create a file named app.js:
const now = new Date();
console.log(`Current time: ${now.toLocaleTimeString()}`);
const numbers = [1, 2, 3, 4];
const total = numbers.reduce((sum, n) => sum + n, 0);
console.log(`Sum: ${total}`);
Run it by passing the filename to node:
node app.js
Output:
Current time: 9:42:15 AM
Sum: 10
Node also has an interactive REPL (Read-Eval-Print Loop) — run node with no arguments to get a prompt much like the browser console, useful for quick checks. Press Ctrl+C twice (or type .exit) to leave it.
Online playgrounds
When you just want to share or sketch an idea without installing anything, browser-based playgrounds are ideal. They run JS instantly and produce a shareable URL.
| Playground | Best for |
|---|---|
| CodePen | HTML + CSS + JS visual demos |
| JSFiddle | Quick front-end snippets |
| StackBlitz | Full Node + framework projects in-browser |
| CodeSandbox | Larger app prototypes and templates |
Choosing how to run
| Method | Environment | Setup | Best for |
|---|---|---|---|
| Browser console | Browser | None | Quick experiments, debugging, DOM |
<script> in HTML | Browser | An .html file | Web pages, UI behavior |
| Node.js | Terminal | Install Node | Scripts, servers, tooling, tests |
| Online playground | Browser | None | Sharing, prototyping |
Best Practices
- Reach for the console for one-off experiments, but move anything you want to keep into a file.
- Load page scripts with
deferortype="module"so they run after the DOM is ready instead of blocking parsing. - Prefer external
.jsfiles over large inline scripts — they are cacheable, reusable, and easier to lint. - Use
type="module"for new front-end code to get strict mode andimport/exportfor free. - Pin a current LTS version of Node so your local runtime matches what CI and production use.
- Keep a playground link handy for sharing reproducible bug reports and small demos.