JavaScript Get Started
To get started with JavaScript you need nothing more than a text editor and a web browser. This chapter shows the three common places your code can run: inside an HTML page, in the browser console and in Node.js.
The <script> Tag
In an HTML page, JavaScript goes inside a <script> element. Placing scripts at the end of <body>, or using the defer attribute, lets the HTML load first so your code can find the elements.
<!DOCTYPE html>
<html>
<body>
<h1 id="title">Hello</h1>
<script>
document.getElementById("title").textContent = "Hello from a script!";
</script>
</body>
</html>Hello from a script!
External JavaScript Files
For real projects, keep code in a separate .js file and link it with the src attribute. This keeps HTML clean and lets the browser cache the script. The type="module" attribute enables modern import/export and implies defer.
<!-- index.html -->
<script src="app.js" defer></script>
<!-- or as an ES module -->
<script type="module" src="main.js"></script>The Browser Console
Every browser has developer tools. Press F12 (or Ctrl+Shift+J / Cmd+Option+J) and open the Console tab. You can type JavaScript there and see results immediately, which is great for quick experiments and debugging.
console.log(2 + 3);
console.log("Today is", new Date().getFullYear() >= 2024 ? "modern times" : "the past");
console.error("Errors show in red");5 Today is modern times Errors show in red
Running JavaScript with Node.js
Install Node.js (version 20 or newer) from nodejs.org, save code into a file and run it from a terminal with node file.js. Browser-only features like document do not exist in Node, but the core language is identical.
// hello.js
console.log("Running on Node", process.version);
// terminal:
// node hello.jsRunning on Node v20.11.0
Recommended Tools
- A code editor such as Visual Studio Code
- A modern browser (Chrome, Firefox, Edge or Safari)
- Node.js 20+ for running scripts outside the browser
- The Try it Yourself editor on this site for quick tests
Frequently asked questions
Where should I put the script tag in HTML?
Put it at the end of the body, or in the head with the defer attribute. Both make sure the HTML is parsed before your code runs.
What is the difference between defer and async?
Both download the script without blocking. defer runs scripts in order after the HTML is parsed; async runs each script as soon as it downloads, in any order.
Can I run JavaScript without a browser?
Yes. Node.js, Deno and Bun all run JavaScript outside the browser, on your computer or a server.