Running Your First Code
Where to type and run JavaScript, how programs execute top to bottom, and how to write comments.
Good news: you already have everything you need to run JavaScript. Every web browser ships with it.
Try this right now: press F12 (or right-click any page → Inspect), click the Console tab, type 2 + 2, and hit Enter. The browser answers 4. You just ran JavaScript.
Before we go further, one core idea: a program is simply a list of instructions. JavaScript reads them top to bottom, one line at a time, doing exactly what each line says — no more, no less. Every program you'll ever write builds on that simple rule.
Three common places to write and run JavaScript:
- The browser console — great for quick one-line experiments (like the
2 + 2you just tried) - Online playgrounds — sites like CodePen or StackBlitz give you an editor and instant results. The interactive exercises here on LingoCode work the same way
- Files on your computer — real projects live in files ending in
.js, run with Node.js or loaded by a web page
JavaScript runs top to bottom. What does this print?
console.log("one");
console.log("two");
console.log("three");- Aone two three
- Bthree two one
- Cone two three
- DThe order is random
Sometimes you want to leave a note in your code — for your future self, or for teammates. That's what comments are for: lines the computer completely ignores.
// This is a comment — JavaScript skips this line entirely
console.log("This line runs"); // comments can also sit at the end of a line
/*
This is a multi-line comment.
Everything between the markers is ignored,
no matter how many lines it spans.
*///— everything after it on that line is ignored/ ... /— everything between the markers is ignored
Programmers also use // to temporarily switch a line off while testing — called commenting out a line.
What does JavaScript do with a line that starts with //?
- AIt ignores the line completely
- BIt prints the line to the console
- CIt runs the line twice
- DIt throws an error
One more thing you'll meet on day one: error messages. Everyone — beginner and expert — sees them constantly. They're not scoldings; they're the computer telling you exactly what confused it.
console.lg("hello");
// TypeError: console.lg is not a functionThe message says it plainly: there's no lg on console (we misspelled log).
Two habits that will save you hours:
- Read the message — it usually names the exact problem and the line number
- Check capitalisation — JavaScript is case-sensitive:
console,Console, andCONSOLEare three different names, and only the first one exists
Add the marker that turns this line into a comment:
___ TODO: add a welcome message here
console.log("App started");One line is commented out. What prints?
console.log("start");
// console.log("middle");
console.log("end");- Astart middle end
- Bstart end
- Cmiddle
- DError
- You can run JavaScript right now in the browser console (
F12→ Console), in online playgrounds, or from.jsfiles with Node.js - A program is a list of instructions, executed top to bottom, one line at a time
- Comments (
//and/ /) are notes the computer ignores - Error messages are helpful, not scary — read them, and watch your capitalisation
Next: teaching your programs to remember things with variables.