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.
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#
- Open any web page in Chrome, Edge or Firefox.
- Press
F12(orCtrl+Shift+Jin Chrome and Edge,Cmd+Option+Jon macOS), and click the Console tab. - Type
console.log("Hello!")and pressEnter.
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:
<!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>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.
- Download the LTS (long-term support) version from nodejs.org and install it. On Windows you can also run
winget install OpenJS.NodeJS.LTS. - Open a new terminal and check the installation:
node --version- Create a file
hello.js:
console.log("Hello from Node.js!");- Run it from the folder that contains it:
node hello.jsHello 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?#
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
console.log("Asha");
console.log("Hyderabad");Asha
HyderabadIf 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?