Skip to content
Laviyx Learn

Try

JavaScript course menu

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

Set up JavaScript: run code in the browser developer console, add scripts to an HTML page, install Node.js and run .js files from the terminal, and choose a code editor such as VS Code.

Beginner4 min readUpdated
On this page

You can start writing JavaScript in under a minute, because every browser already includes a JavaScript engine. For larger programs, you will also install Node.js, which runs JavaScript outside the browser.

Option 1: the browser console#

  1. Open any web page in Chrome, Edge or Firefox.
  2. Press F12 (or Ctrl+Shift+J in Chrome and Edge, Cmd+Option+J on macOS), and click the Console tab.
  3. Type console.log("Hello!") and press Enter.

The console runs each line immediately and shows the result. It is perfect for quick experiments. The console also shows errors from the page, which you will use constantly when debugging.

Option 2: a script in an HTML page#

Create a folder with two files:

index.html (HTML)
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>My first script</title>
    <script src="app.js" defer></script>
  </head>
  <body>
    <h1>Open the console to see the output</h1>
  </body>
</html>
app.js (JavaScript)
console.log("app.js is running!");

Open index.html in your browser, then open the console: you will see the message. Edit app.js, save, and reload the page to run it again. The defer attribute is explained in Adding CSS and JavaScript.

Option 3: Node.js#

Node.js runs JavaScript files from the terminal, without a browser.

  1. Download the LTS (long-term support) version from nodejs.org and install it. On Windows you can also run winget install OpenJS.NodeJS.LTS.
  2. Open a new terminal and check the installation:
Terminal
node --version
  1. Create a file hello.js:
hello.js (JavaScript)
console.log("Hello from Node.js!");
  1. Run it from the folder that contains it:
Terminal
node hello.js
Output
Hello from Node.js!

Running node with no file name opens an interactive prompt (a REPL), like the browser console. Type .exit or press Ctrl+C twice to leave.

Which one should you use?#

TaskBest tool
Quick experimentsBrowser console or Node REPL
Lessons in the fundamentals sectionsNode.js files (output is exactly as shown on this site)
Working with web pages (the DOM)An HTML file with a script, in the browser
Servers and toolsNode.js

The core language is the same everywhere. What differs is the environment: browsers provide document and window for web pages, while Node.js provides access to files, the network and the operating system.

A code editor#

Use Visual Studio Code (free). It understands JavaScript out of the box: colours, auto-complete, error hints and a built-in terminal (Ctrl+backtick) where you can run node.

How output looks on this site#

Every example in this course was run with Node.js, and the Output box shows exactly what Node printed. Browser consoles show the same values, but display objects and arrays in an interactive, expandable form instead of plain text.

Practice#

Practice exercise

Install Node.js, create greet.js that prints your name and your city on two lines, and run it with node greet.js.

Show solution
greet.js (JavaScript)
console.log("Asha");
console.log("Hyderabad");
Output
Asha
Hyderabad

If the terminal says node is not recognized, close and reopen it after installing, so it picks up the updated PATH.

Key takeaway#

Finished this lesson?