Learning Goals
3 minBy the end of this lesson you will be able to:
- Explain in one sentence what Node.js is and why it exists.
- Check your Node version in a terminal with
node --version. - Write a
.jsfile and run it from the terminal withnode hello.js. - Name two things Node can do that browser JavaScript cannot.
Warm-Up · Where Does It Run?
5 minIn Level 2 you ran JavaScript inside the browser — clicking buttons, changing the page, fetching data. The browser gave you helpers like document and window.
Look at this two-line script. Read it, then predict:
console.log('Hello from JavaScript!')
document.querySelector('h1')Predict together
- Which line prints a message you can read?
- The word
documentmeans “the web page”. If there is no web page, what might go wrong on line 2?
Reveal
Line 1 always works — it just prints text. Line 2 only works inside a browser, because document is the page. Run these two lines outside a browser and line 2 crashes with ReferenceError: document is not defined. There is no page to query. That “outside a browser” place is exactly what we meet today.
New Concept · JavaScript Leaves the Browser
12 minThink of a fish that has only ever lived in one tank. It can swim, but only there. Node.js is like moving that fish into a whole river — same fish, far more places to go.
For years, JavaScript could only run inside a browser. Then people took the browser's JavaScript engine — the part called V8, the same one inside Chrome — and wrapped it so it could run anywhere on your computer. That wrapper is Node.js.
Node.js — a program that runs JavaScript files directly on your computer, without any browser. We call it a runtime: the thing that runs your code.
The smallest possible Node program
Make a file called hello.js with one line inside:
console.log('Hello from Node!')Then, in a terminal, run it by typing:
$ node hello.jsYou will see the message printed straight into the terminal:
Hello from Node!Browser JavaScript vs Node JavaScript
Same language, different powers:
In the browser
Has window, document and the page (the DOM).
Can click buttons and change what you see.
Cannot freely read files on your computer.
In Node.js
No window, no document — there is no page.
Can read and write files, and talk to the network directly.
Can run a web server — the goal of this whole level.
Why it matters
Every backend in Level 3 runs on Node. So does the tooling you already use — Vite, npm, even this course's build. Learn Node once, and you write JavaScript for both sides of a website: the browser and the server.
Worked Example · Your First Node Run
12 minFollow along on your own machine. You will need a terminal — the built-in one in VS Code is perfect. Open it with Terminal → New Terminal from the top menu.
Step 1 — Check Node is installed
Type this and press Enter:
$ node --versionIf Node is installed, you will see a version number:
v22.14.0No version, or a “command not found” error? Go to nodejs.org, download the LTS version (LTS = “Long-Term Support”, the stable one), install it, then close and reopen the terminal and try again.
Step 2 — Make a file
In VS Code, make a new file called hello.js. Type this in — a friendly greeting for Aisyah:
const name = 'Aisyah'
console.log('Selamat datang, ' + name + '!')Step 3 — Run it
Back in the terminal, run the file by name:
$ node hello.jsSelamat datang, Aisyah!No browser. No HTML. Node read your file, ran it top to bottom, and printed the result. That is the whole idea.
Step 4 — Do something the browser can't
Node gives you a special object called process with facts about the computer it is running on. The browser has no such thing. Add these two lines to hello.js:
console.log('Node version:', process.version)
console.log('Running on:', process.platform)Run it again with node hello.js. Now you also see:
Selamat datang, Aisyah!
Node version: v22.14.0
Running on: win32What changed? Your script now reports facts about your machine — the Node version and the operating system (win32, darwin for Mac, or linux). Browser JavaScript can never reach that.
Try It Yourself
13 minThree tasks, all run with node <filename> in the terminal. Keep each one in its own file.
Task 1 — Make it yours
Open
hello.js. Change the name and greeting to your own, and add a line about your favourite food, for example:console.log('My favourite food is nasi lemak.')Run it and check both lines appear in the terminal.
Task 2 — A little maths
Make a new file
total.js. Store the price of two snacks in variables (in RM), add them, and print the total. Start from this shape and fill in the blanks:const tehTarik = 2.5 const kuih = 1.5 const total = tehTarik + kuih console.log('Total: RM', total)Run
node total.js. Did it printTotal: RM 4?Task 3 — Introduce yourself
Make
about.js. Using a template literal (backticks and${ }from Level 2), print three lines about yourself — your name, your town, and one thing you want to build. Run it with Node.
Mini-Challenge · The Mamak Receipt
8 minWrite a script called receipt.js that prints a tidy receipt for three items from a mamak stall. Use everything so far: variables, arithmetic, and running the file with Node.
It works if: running node receipt.js prints three item lines and a correct total in RM at the bottom.
Reveal a sample answer
// receipt.js — a mamak order for Wei Jie
const rotiCanai = 1.5
const milo = 3.0
const magaGoreng = 7.5
const total = rotiCanai + milo + magaGoreng
console.log('--- Mamak Receipt ---')
console.log('Roti canai: RM', rotiCanai)
console.log('Milo ais: RM', milo)
console.log('Maggi goreng: RM', magaGoreng)
console.log('---------------------')
console.log('Total: RM', total)Running it prints:
--- Mamak Receipt ---
Roti canai: RM 1.5
Milo ais: RM 3
Maggi goreng: RM 7.5
---------------------
Total: RM 12Your item names and prices will differ — that is fine. The point is a running Node script that does real work and prints a correct total.
Recap
3 min- Node.js runs JavaScript on your computer, with no browser needed.
- It uses V8 — the very same engine that runs JavaScript inside Chrome.
- Run a file from the terminal with
node <filename>. - Node has no
documentorwindow— there is no web page. - Node can reach files, the network, and machine facts (via
process) — the browser can't.
New words
- Runtime — the program that runs your code (Node is a JavaScript runtime).
- Terminal (CLI) — the typing window where you run commands like
node. - V8 — the fast JavaScript engine shared by Chrome and Node.
Homework
4 min to brief · ~20 min to doRequired
- Make sure Node is installed —
node --versionshould show a version number. If not, install the LTS from nodejs.org. - Write a file
facts.jsthat prints three facts about you, plus one line showingprocess.version. Run it, take a screenshot of the terminal output, and bring it to next lesson.
Optional stretch
- Type
nodeon its own (no filename) and press Enter. This opens the Node REPL — a live JavaScript prompt. Try2 + 2, then'nasi' + ' lemak'. Type.exitto leave. - Read the first paragraph of the Node.js “About” page. Write down one new word and what you think it means.