How do you run JavaScript?
Short answer: there are three common ways. To try a line, open your browser's console with F12 and type it. To run a .js file on your computer, install Node.js and write node file.js in the terminal. To run JavaScript in a web page, load it with a <script> tag in an HTML file.
node hello.js # run a file with Node.js
node -p "[1, 2, 3].map(n => n * 2)" # [ 2, 4, 6 ]You don't install JavaScript itself. Every browser already runs it, and Node.js lets you run it outside the browser.
node runs a file or a single line in the terminal, and code that uses document or window has to run in the browser.
Every way to run JavaScript
| Where | How | Best for |
|---|---|---|
| Browser console | F12 (or Cmd + Option + J on Mac) → Console | Trying a line, debugging a page |
| HTML file | <script src="app.js"></script> and open the file in the browser | Web pages that use document |
| Terminal with Node.js | node app.js | Scripts, servers, tools |
| One line in the terminal | node -p "2 + 3" or node -e "console.log(5)" | Quick tests |
| Interactive console | node, then type code; .exit to leave | Exploring step by step |
| Code editor | VS Code: open the terminal and run node app.js | Projects with many files |
| Online playground | CodePen, JSFiddle, StackBlitz | Sharing an example without installing anything |
Run JavaScript in the browser console
- Open any page in Chrome, Edge, Firefox or Safari.
- Press F12, or Cmd + Option + J on Mac (in Safari, first turn on Show features for web developers in Settings → Advanced, then use Cmd + Option + C).
- Click the Console tab, type your code and press Enter:
console.log("Hello from the console");
2 + 3 // 5
document.title // the title of the page you are onFor several lines, press Shift + Enter to add a line without running it. The code runs on the page you have open, and it's gone when you reload.
