Hello, world!
Run your first JavaScript script in a browser page and with Node.js, using script tags, src files, and console.log.
- 01Run code in a pagePlace an inline script where the browser can run it safely.
- 02Move code to app.jsUse a script tag with src and know what defer changes.
- 03Run code with NodeCreate hello.js, run node hello.js, and recognize the REPL.
Your first script
Hello, world! is the tiny first program many developers write in a new language. It does not build an app yet. It proves the most important thing: your code is in the right place, the right program is running it, and you can see the result.
In this lesson you will run JavaScript in two hosts: a browser page and Node.js. You met the idea of hosts in Engines, runtimes & hosts; now you will use it. The same JavaScript language can run in both places, but each host gives it different tools.
A script is a file or block of JavaScript instructions. The browser runs scripts from HTML <script> tags. Node.js runs scripts from the terminal with commands such as node hello.js.
Imagine reading a cookbook. When a note is taped between two paragraphs, you stop reading and follow the note. A script tag works like that: the browser reaches it, runs the instructions, then continues reading the HTML.
- In real life: A reader moves through a recipe line by line
- In JavaScript: The browser parses HTML from top to bottom
- In real life: A note says, stop and follow these instructions
- In JavaScript: A
<script>tag tells the browser to run JavaScript - In real life: A sticky note says, see the card named app.js
- In JavaScript:
<script src="app.js">loads another file
Where the analogy stops: A person can flip ahead in a book whenever they want. A normal browser parser follows stricter timing rules, so a script may run before later HTML exists unless you place it later or use defer.
You will also use console.log. It prints a message for developers, not for visitors on the page. Earlier, the Why JavaScript? lesson used it in the first five lines; here you will slow down and learn exactly what it can print.
The script tag
INTERACTIVEThe browser reads HTML from top to bottom. When it meets a normal inline script, it runs the code right there. That timing matters. If the script tries to find a button before the browser has read the button's HTML, the answer is null: nothing was found.
<!doctype html>
<html>
<body>
<h1>Hello page</h1>
<script>
console.log("Hello, world!");
</script>
</body>
</html>Inline scripts are useful for tiny experiments and examples because the JavaScript is visible right where it runs. In real projects, you quickly move code into its own file so HTML and JavaScript stay readable.
<!doctype html><html> <head> <script> const button = document.querySelector("button"); console.log(button); </script> </head> <body> <button>Press me</button> </body></html>No logs yet. Press Run page.
The browser reads top to bottom. A normal head script runs before the body, so querySelector cannot find the button yet and logs null.
Try the first setup: script in <head>. It logs null because the browser has not reached the button yet. Move the script to the end of <body>, and it logs Press me. Use defer with an external script, and it also logs Press me because deferred scripts run after the HTML has been parsed.
External scripts with src
COMPAREAn external script keeps JavaScript in a separate file. The HTML contains a pointer, and the browser requests the file named by src. That pointer is the sticky note version of the script tag: see the instructions in app.js.
| Question | Inline script | External script with src |
|---|---|---|
| Where is the JavaScript? | Between the opening and closing script tags | In another file, usually app.js |
| Best for | Tiny examples and one-off demos | Real projects and reusable code |
| Can it use defer? | No, defer is for external scripts | Yes: <script src="app.js" defer></script> |
| What if the tag has src and text inside? | Not applicable | The inline text is ignored for execution |
<!doctype html>
<html>
<body>
<h1>Hello page</h1>
<script src="app.js"></script>
</body>
</html>console.log("Hello, world!");If a script tag has src, code written between its opening and closing tags is ignored for execution. Write either <script src="app.js"></script> or an inline script, not both.
console.log
STEP THROUGHconsole.log(...) prints values to a developer console. It is how you leave breadcrumbs for yourself while learning: did this file run, what value did this variable hold, which branch happened?
It can print strings, numbers, expressions, several arguments separated by commas, and strings built from variables. Predict each line below, then step through the recorded run. Change total and watch only the lines that read total change.
Predict each log. Then step through the real recorded run and compare your prediction with the console output.
script
const total = 5;console.log("Hello, world!");console.log(2 + 3);console.log("Total:", total);console.log(`${name} has ${total} tabs open.`);In a browser console or the Node REPL, console.log("Hi") prints Hi, then the prompt may show undefined. That extra word is the return value of console.log itself. It is not an error.
Running with Node.js
TERMINALThe browser is one kitchen for JavaScript. Node.js is another. Running with Node is like taking the recipe out of the cookbook and cooking it in a different kitchen: the JavaScript language is familiar, but the tools around it change.
The recipe can say “chop onions” in either kitchen, but the drawers contain different tools. JavaScript expressions such as 2 + 3 work in both hosts; page tools such as document.querySelector only make sense in a browser page.
- In real life: Recipe instructions
- In JavaScript: The JavaScript language: strings, numbers, expressions
- In real life: Cookbook kitchen
- In JavaScript: Browser host:
document,window, buttons - In real life: Apartment kitchen
- In JavaScript: Node host: terminal input/output, files, packages
Where the analogy stops: Some tools exist in both kitchens, such as timers and console.log. Other tools belong to one host: document is browser-only, and Node's fs module is Node-only.
$ cat hello.js
console.log("Hello from Node!");
console.log(2 + 3);
$ node hello.js
Hello from Node!
5
$ node
Welcome to Node.js ...
> console.log("Hi from the REPL")
Hi from the REPL
undefined
> .exitType node hello.js to run a file. Type node by itself and Node opens the REPL: an interactive prompt. Leave it with .exit or by pressing Ctrl + C twice.
console.log("Hi")document.querySelector("button")- reading a file with fs
alert("Saved!")2 + 2setTimeout(() => console.log("later"), 1000)
Sort each feature by the host that provides it. The explanations remind you which tools belong to the language and which belong to a host.
A plain Node script does not create a web page, so document and window are not defined. Running console.log(document.querySelector("button")); in Node throws ReferenceError: document is not defined.
Where you'll use this
You use script tags whenever HTML needs behavior. A portfolio might load app.js to open a menu. A form might log test values while you build it. A coding exercise might ask you to run node solution.js so the terminal can check your answer.
<!-- index.html -->
<button id="save">Save</button>
<script src="app.js" defer></script>
// app.js
const save = document.querySelector("#save");
console.log("Save button:", save);This pattern is intentionally simple: keep HTML in index.html, keep JavaScript in app.js, use defer so elements exist, and use console.log to verify what your code found. Later lessons will replace logs with real page updates and user prompts.
Common misconceptions
“A script in the head can see the whole page.”
Not unless it waits. A normal head script runs before the body is parsed, so later elements do not exist yet. Use the end of body or defer.
“src adds to the inline code.”
No. When src is present, inline code inside the same script element is ignored for execution.
“console.log changes the page.”
It only writes to the developer console. Visitors do not see it as page content.
“Node and the browser have all the same globals.”
They share the JavaScript language, but the hosts differ. Browser code has document; Node code has file-system tools.
“Typing node always runs my file.”
node alone opens the REPL. To run a file, include its name: node hello.js.
Practice: run Hello, world
5 EXERCISESCreate an index.html file with an inline script that logs Hello, world!.
<script>
console.log("Hello, world!");
</script><script>
console.log("Hello, world!");
</script>The script tag tells the browser to run the JavaScript inside it. The console prints Hello, world!.
Move the log into app.js and load it from index.html with src.
<script src="app.js"></script>
// app.js
console.log("Hello, world!");The browser sees src="app.js", loads that file, and runs its contents. Keeping code in app.js is easier to maintain.
What does this line print?
console.log("A", 1 + 1, "B");It prints A 2 B. The expression 1 + 1 becomes 2, then console.log prints the three arguments in order.
Create hello.js and run it from the terminal. What command do you type?
console.log("Hello from Node!");node hello.jsFrom the folder containing hello.js, node hello.js runs the file and prints Hello from Node!.
This tag does not log hi. Which part is ignored?
<script src="app.js">console.log("hi")</script><script src="app.js"></script>
<!-- Put console.log("hi") inside app.js instead. -->When src is set, the inline console.log("hi") is ignored for execution. Put the code in app.js, or remove src and make the script inline.
Quiz: check your understanding
7 QUESTIONSQuestion 1 of 7What does a plain
<script>tag do in an HTML page?Choose an answer to see the explanation.
Question 2 of 7What does this print?
Read the code, then predictconsole.log("A", 1 + 1, "B");Choose an answer to see the explanation.
Question 3 of 7Why put
<script src="app.js" defer></script>in the head?Choose an answer to see the explanation.
Question 4 of 7What happens if a script in the head runs
document.querySelector("button")before the button exists?Choose an answer to see the explanation.
Question 5 of 7Which command runs a JavaScript file named hello.js with Node?
Choose an answer to see the explanation.
Question 6 of 7What does this Node script throw?
Read the code, then predictconsole.log(document.querySelector("button"));Choose an answer to see the explanation.
Question 7 of 7What is the Node REPL?
Choose an answer to see the explanation.
Key takeaways
<script>runs JavaScript in a browser page.srcloads an external JavaScript file;deferwaits until the HTML is parsed.console.logprints developer messages: strings, numbers, expressions, and multiple arguments.node hello.jsruns a JavaScript file in Node;nodealone opens the REPL.- Browser hosts have
document; plain Node scripts do not.
Remember the one-liner.
A script is JavaScript handed to a host: HTML hands it to the browser; node hello.js hands it to Node.
Up next: Statements, comments & semicolons.