Menu

How to Run JavaScript: Browser Console, Node.js, and HTML

The practical ways to run JavaScript in 2026 — the browser console, a script tag in HTML, Node.js from the terminal, and when to reach for each.

Three Places JavaScript Runs

JavaScript doesn't run on its own — it needs a host environment that understands the language. In 2026 there are three hosts you'll actually use:

  • A browser. Every browser ships with a JavaScript engine (V8 in Chrome and Edge, SpiderMonkey in Firefox, JavaScriptCore in Safari). Paste code into DevTools and it runs.
  • Node.js. A standalone runtime that lets JavaScript run outside the browser — from the terminal, on servers, inside build tools.
  • Online playgrounds and editor widgets (like the ones on this page). Under the hood these are just one of the above, wrapped in a web UI.

The language is the same everywhere. What changes is what the code can reach — a browser gives you document and window, Node gives you the filesystem and the network. Start with whichever is easier for your situation; you can switch any time.

Option 1: The Browser Console

The fastest way to run a line of JavaScript is the browser's DevTools console. Open any web page, press F12 (or Cmd+Option+I on Mac), click the Console tab, and type:

index.js
Output
Click Run to see the output here.

Press Enter after each line. The console prints the return value of expressions and anything you pass to console.log. You can define variables, call functions, and inspect the current page — document.title will give you the current tab's title.

The console is great for:

  • Testing a snippet in two seconds.
  • Poking at a live website's DOM.
  • Debugging code that's already running on a page.

What you type in the console is thrown away when you close the tab. For anything you want to keep, use a file.

Option 2: A <script> Tag in an HTML File

To run JavaScript as part of a web page, drop it in an HTML file. Save this as index.html and double-click it to open in your browser:

<!doctype html>
<html>
  <body>
    <h1 id="title">Loading...</h1>
    <script>
      document.getElementById("title").textContent = "Hello from a script";
      console.log("Script ran");
    </script>
  </body>
</html>

The <script> tag tells the browser "run this as JavaScript." The code has access to the page it's embedded in, so document.getElementById(...) can reach the <h1> above it. Open DevTools to see the console.log output.

For anything longer than a few lines, move the code to a separate file and link it:

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

Put the <script> tag near the end of <body>, or add defer to it, so it runs after the page's HTML is parsed. More on module scripts and loading order in the Modules chapter.

Option 3: Node.js From the Terminal

When you want JavaScript without a browser — a script that renames files, a small server, a quick data-crunching job — use Node.js. Install it from nodejs.org (pick the LTS version), then check it works:

node --version

Save a file as script.js:

index.js
Output
Click Run to see the output here.

Then from the same folder, run:

node script.js

Output prints straight to the terminal. No HTML, no browser, no build step. This is how most "real" JavaScript development outside the browser happens — Node also powers the tools (bundlers, test runners, linters) that sit around a browser project.

You can also run Node with no file at all. Just type node and press Enter — you get an interactive prompt (a REPL) where each line runs as you type it. Handy for trying things, like the browser console but in your terminal.

Option 4: The Editor on This Page

Every editor-javascript block on Coddy is a live editor. Edit the code, hit Run, see the output. It's the friendliest option while you're learning — no install, no setup, no tab-switching:

index.js
Output
Click Run to see the output here.

Change "world" to your name and rerun it. That's the whole feedback loop — edit, run, read the output. Most of this tutorial is designed around it.

Which One Should You Use?

Pick whichever matches what you're doing:

  • Trying a one-liner, or inspecting a live page — browser console.
  • Building something with a webpage around it — HTML file with a <script> tag.
  • Writing a script, a tool, or a server — Node.js from the terminal.
  • Following this tutorial — the editor blocks right here.

A few things that trip beginners up:

  • Code in Node.js has no document or window. Those are browser things. Calling document.getElementById(...) in Node throws ReferenceError: document is not defined.
  • Code in the browser has no access to your filesystem. Browsers sandbox pages for security. If you want to read a local file, you're in Node territory.
  • console.log works in all three. It's the universal "print something" for JavaScript.

A Sanity-Check Script

Run this in whichever environment you picked. If you see the three lines of output, you're ready for the next page:

index.js
Output
Click Run to see the output here.

Three features in one snippet: console.log, a built-in Date object, and an array method with an arrow function. You'll meet each of them properly in the coming chapters.

Next: Syntax and Semicolons

Now that code is actually running somewhere, the next question is what JavaScript syntax looks like — statements, expressions, and the perennial argument about whether to put semicolons at the end of every line. That's next.

Frequently Asked Questions

How do I run JavaScript code?

You have three easy options. Paste it into a browser's DevTools console and hit Enter. Put it in an HTML file inside a <script> tag and open the file. Or install Node.js and run node script.js from a terminal. All three run the same language — they just wrap it in different environments.

How do I run a JavaScript file from the command line?

Install Node.js from nodejs.org, save your code as script.js, then run node script.js in a terminal from the same folder. console.log(...) output prints straight to the terminal. No HTML, no browser, no build step required.

How do I run JavaScript in Chrome?

Open any web page, press F12 (or Cmd+Option+I on Mac), and click the Console tab. Type any expression and press Enter — it runs immediately against that page. It's the fastest way to test a snippet, inspect the DOM, or poke at a live site.

Learn to code with Coddy

GET STARTED