How to run JavaScript
Try the browser console
Open your browser's developer tools, select Console, enter an expression, and press Enter. The console is ideal for short experiments.
2 + 3
console.log("JavaScript is ready");
Run a script in a page
Save these files together, open index.html, and inspect both the page and console. defer waits until the HTML has been parsed.
<!-- index.html -->
<p id="status">Waiting...</p>
<script src="app.js" defer></script>
// app.js
document.querySelector("#status").textContent = "Running!";
Run JavaScript with Node.js
Install a current Node.js release, save the code as app.js, and run it from that directory. Node has no page or document object.
// app.js
const message = "Hello from Node.js";
console.log(message);
// Terminal:
node app.js
Choose the right environment
- Use the console for quick checks
- Use a script element for browser behavior and DOM work
- Use Node.js for command-line programs, servers, and tooling
- Check versions with
node --versionand keep developer tools open while learning