Getting Started
JavaScript can't run by double-clicking a file, the way you might run a program on your computer. It needs a host — something that already knows how to execute it. You have three, and you already own all of them.
💡 The Bottom Line
Three places to run JavaScript today: the browser console (for scratch work), a <script> tag in an HTML page (for real projects), and Node.js (for running JavaScript outside a browser entirely). This lesson uses the first two.
1. The console — your scratchpad
You used this in Lesson 1. Every browser ships a JavaScript console for experiments that don't need saving. It's the fastest way to test a one-line idea.
console.log("Hello, learner");
console.log(2 + 2);
console.log("2" + 2); // not what you'd expect — more on this next lesson
console.log() is the single most-used piece of JavaScript you will ever write. It
prints whatever you hand it. Long after you've learned everything else in this track, you'll still be
sprinkling console.log() around to see what your code is actually doing.
2. The <script> tag — your real projects
A console session disappears when you close the tab. To build something that lasts, JavaScript
lives inside an HTML file, in a <script> tag:
<!DOCTYPE html>
<html>
<body>
<h1>My Page</h1>
<script>
console.log("This runs when the page loads");
</script>
</body>
</html>
Notice the script sits just before the closing </body> tag. That's
deliberate, not decoration.
⚠️ Watch Out
If your script tries to grab an element from the page (you'll do this in Lesson 6) and it's
placed in the <head>, it will run before that element exists — and fail.
Placing scripts at the end of <body>, or using the defer attribute,
guarantees the page is built first.
3. External files — keeping things tidy
Once a script grows past a few lines, pull it into its own file, the same way you'd separate CSS from HTML:
<script src="app.js" defer></script>
The defer attribute tells the browser: "download this whenever, but only run it once
the whole page has finished loading." It's the modern default for external scripts.
Comments — notes to your future self
// this is a single-line comment
/* this is a
multi-line comment */
Comments aren't executed. Good code has just enough of them to explain why, not what — the code itself already says what it does.
🙋 There Are No Dumb Questions
Q: What's Node.js, then, if the browser already runs JavaScript?
A: Node.js takes the same JavaScript engine Chrome uses (called V8) and lets it run directly on
your computer or a server — no browser, no web page. That's how JavaScript ended up building
back-end servers, command-line tools, and even this kind of site's build scripts. Same language,
different host.
🔧 Try It Yourself
Open your browser console again (right-click → Inspect → Console) and run all three lines from
the "scratchpad" example above. Before you press Enter on the third line, guess out loud what
"2" + 2 will print. Were you right?
✅ Quick Check
Why do developers usually place their <script> tag right before </body>, or use the
defer attribute?
You just saw "2" + 2 do something a calculator never would. That's not a bug — it's
JavaScript's type system, and it's exactly what Lesson 3 explains.