Hello World (first programs) · Beginner · 15 min
Hello World in JavaScript: run it in the browser and in Node.js
By Padding · Updated 18 Nov 2026
Node.js 26.13.1 LTS · npm 11.20 · node:26 image
You will run JavaScript in a browser console and in a web page, install Node.js 26 LTS, write and run hello.js, greet a name passed on the command line, and turn the folder into an npm project with a start script.
On this page
JavaScript is the only programming language that every web browser runs. Every interactive web page uses it, so if you build anything for the web, you will write some. Since Node.js arrived in 2009, the same language also runs outside the browser: web servers and APIs, build tools such as bundlers and linters, command-line programs, and desktop apps built with Electron, such as Visual Studio Code.
That gives JavaScript something most languages do not have: one language, two very different hosts. The language itself (variables, functions, strings, loops) is the same everywhere. What changes is what the host gives your code. A browser gives you the page, as document, and the window. Node.js gives you files, the network and the command line, through process and modules such as node:fs. Code that uses only the language runs in both. Code that uses one host's objects fails in the other, and you will see that happen on purpose in this tutorial.
You start in a browser with nothing to install, then install Node.js 26 LTS, write a program in a file, read a name from the command line, and finish with an npm project that runs with npm start.
Step 1 · Say hello in the browser console
Every desktop browser has developer tools with a console, where you can type JavaScript and see the result straight away. Open any web page, or a blank tab, and open the console:
- Chrome or Edge: press
Ctrl+Shift+Jon Windows and Linux, orCmd+Option+Jon macOS. - Firefox: press
Ctrl+Shift+Kon Windows and Linux, orCmd+Option+Kon macOS. - Safari: first turn on the developer features in Safari's settings (Advanced tab), then press
Cmd+Option+C.
Click in the console, type this line and press Enter:
console.log('Hello, world!');console is an object the host provides for writing messages, and log is a function on it that prints whatever you pass in. The text between the single quotes is a string. The semicolon ends the statement. JavaScript inserts semicolons for you in most places, but writing them avoids a few surprising cases.
Type the line rather than pasting it. Chrome (and Firefox) may block pasting the first time you use the console, until you type allow pasting, a guard against people being tricked into running code they do not understand. Now try one line that only a browser understands:
document.titleCheck it worked
The console shows Hello, world! on its own line. The console also shows undefined under it: the console prints the value of each line you type, and console.log returns no value. The document.title line shows the title of the tab you are on, in quotes. document is the page itself, and it exists only in the browser.
Phones are different: Chrome on Android has no developer console. If a phone is your only device for now, read the browser steps and do the Node.js steps on any computer you can use, including an older laptop; Node.js runs well on modest hardware.
Step 2 · Install Node.js 26 LTS
Node.js 26 is the current Long Term Support line, which gets security fixes for years. Install it for your system:
- Ubuntu: follow install Node.js 26 LTS on Ubuntu, which sets it up through apt so it stays patched.
- Windows and macOS: download the LTS installer from nodejs.org/en/download and run it. It installs
nodeandnpmand puts them on your path. The same page also shows how to install through a version manager such as nvm or fnm, which suits you if you will switch between Node.js versions later.
The installer is a single download. If your data is prepaid or metered, download it once on Wi-Fi and keep the file to install on other machines.
Open a new terminal, so it picks up the new path, and check both programs:
node --version
npm --versionCheck it worked
node --version prints v26. followed by two more numbers, such as v26.13.1. npm --version prints a version that starts with 11.. If you see command not found or 'node' is not recognized, close the terminal and open a new one; if it persists, run the installer again.
Step 3 · Write and run hello.js
Make a folder for this tutorial and create a file called hello.js in it. You can type the three lines of code into your editor and save the file in the folder, or run this block, which does both:
mkdir -p ~/hello-js-demo
cd ~/hello-js-demo
cat > hello.js <<'JS'
// hello.js: my first JavaScript program
const greeting = 'Hello, world!';
console.log(greeting);
console.log(`This is Node.js ${process.version}`);
JS
node hello.jsLine by line:
// hello.js: ...is a comment. Everything after//on a line is ignored by JavaScript.const greeting = 'Hello, world!';creates a name,greeting, that refers to the string.constmeans the name cannot be pointed at a different value later.console.log(greeting);prints the value thatgreetingrefers to. Node.js providesconsoletoo, and here it writes to the terminal instead of a developer console.- The last line uses a template literal: a string in backticks, where
${...}is replaced by the value of the expression inside it.processis an object that Node.js provides, andprocess.versionis the version string of the running Node.js.
Notice what is missing. There is no main function, no class and no compile step. Node.js reads the file and runs its top-level statements from top to bottom, the same way the browser runs a script in a page.
Check it worked
The terminal shows two lines: Hello, world! and This is Node.js v26. followed by the rest of your version number. If you see Error: Cannot find module with a path ending in hello.js, your terminal is in a different folder from the file. Run cd ~/hello-js-demo (or the folder you saved it in) and try again.
Step 4 · Greet a name from the command line
Programs become useful when they take input. In Node.js the words you type after the script name arrive in process.argv, an array (a list) of strings. See what it holds:
cd ~/hello-js-demo
cat > args.js <<'JS'
console.log(process.argv);
JS
node args.js Ada LovelaceThe output is a list in square brackets. Item 0 is the full path of the node program, item 1 is the full path of args.js, and the words you typed come next: 'Ada' and 'Lovelace'. Arrays count from 0, so the first word you type is always process.argv[2].
Now replace hello.js so it greets a name, or the world when you give none:
cat > hello.js <<'JS'
const name = process.argv[2] ?? 'world';
console.log(`Hello, ${name}!`);
console.log(`Your name has ${name.length} characters.`);
JS
node hello.js
node hello.js Ada
node hello.js Adébáyọ̀?? is the nullish coalescing operator. If the value on its left is undefined (no word was typed) or null, the expression gives the value on its right instead. name.length counts the string's UTF-16 code units, which for most text is the number of characters.
Check it worked
The first run prints Hello, world! and Your name has 5 characters. The second prints Hello, Ada! and Your name has 3 characters. The third prints Hello, Adébáyọ̀!, which shows that JavaScript strings hold any language's letters. Its count may be higher than the letters you see, because some accented letters are stored as a base letter plus a combining mark, and length counts each part.
Step 5 · See the two hosts side by side
Write a small web page with a script in it, in the same folder:
cd ~/hello-js-demo
cat > hello.html <<'HTML'
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello, JavaScript</title>
</head>
<body>
<h1>Waiting for JavaScript</h1>
<script>
const heading = document.querySelector('h1');
heading.textContent = 'Hello, world!';
console.log('Hello from the page');
</script>
</body>
</html>
HTMLThe <script> element holds JavaScript that the browser runs when it reaches it. It sits at the end of <body>, so the <h1> already exists when the script runs. document.querySelector('h1') finds the first <h1> on the page, and setting its textContent replaces the heading's text. <meta charset="utf-8"> tells the browser how the file is encoded, so text in any language displays correctly.
Open hello.html in your browser: double-click it in your file manager, or drag it onto a browser window. Then open the console as in the first step.
Now ask Node.js which host objects it has. node -e runs the JavaScript you give it in quotes, without a file:
cd ~/hello-js-demo
node -e "console.log(typeof document, typeof process)"
node -e "console.log(document.title)" || echo "exit status $?"typeof gives the type of a value as a string, and gives 'undefined' for a name that does not exist instead of failing.
Check it worked
The page shows the heading Hello, world!, not "Waiting for JavaScript", and the tab title is Hello, JavaScript. The console shows Hello from the page. Type typeof process in that console: it shows 'undefined', because the browser has no process.
In the terminal, the first node -e prints undefined object: Node.js has process but no document. The second fails with ReferenceError: document is not defined and a few lines showing where, then the shell prints exit status 1. This is the error you get when you copy browser code into a Node.js file. There is no page in Node.js, so there is no document.
Step 6 · Make it an npm project with a start script
Real JavaScript projects have a package.json file. It names the project, lists the packages it depends on, and holds scripts that anyone can run with npm. Create one with the default answers:
cd ~/hello-js-demo
npm init -ynpm init -y writes package.json and prints it. Look for the type field: "type": "commonjs". JavaScript has two module systems. CommonJS, with require(), is the one Node.js started with. ES modules, with import and export, are the standard one that browsers also use. The type field decides which one Node.js uses for the .js files in this folder.
Change hello.js to the ES module style, which imports what it needs from Node.js instead of relying on the global process, and run it:
cat > hello.js <<'JS'
import { argv, version } from 'node:process';
const name = argv[2] ?? 'world';
console.log(`Hello, ${name}!`);
console.log(`Running on Node.js ${version}`);
JS
node hello.js Ada || echo "exit status $?"It fails with SyntaxError: Cannot use import statement outside a module, because the folder still says CommonJS. Switch the project to ES modules and add a start script with npm pkg set, which edits package.json for you:
npm pkg set type=module
npm pkg set scripts.start="node hello.js"
npm pkg get type scripts
node hello.js Ada
npm start
npm start -- Gracenpm start runs the command saved under scripts.start. Everything after -- is passed on to that command, so npm start -- Grace runs node hello.js Grace. The node: prefix in the import marks a module built into Node.js, so it can never be confused with a package of the same name from npm.
Check it worked
npm pkg get prints "type": "module" and the scripts, including "start": "node hello.js". node hello.js Ada now prints Hello, Ada! and Running on Node.js v26. with your version. npm start prints two lines starting with >, showing the project name and the command it runs, then Hello, world! and Running on Node.js v26. with your version. npm start -- Grace prints Hello, Grace! in place of Hello, world!. Open package.json in your editor and you will see "type": "module" and the start line in scripts.
Step 7 · Recognise the errors beginners hit
Make two common mistakes on purpose, so you know the messages when they happen by accident. The first misspells console. The second leaves out a closing quote:
cd ~/hello-js-demo
cat > typo.js <<'JS'
consol.log('Hello');
JS
cat > quote.js <<'JS'
console.log('Hello);
JS
node typo.js || echo "exit status $?"
node quote.js || echo "exit status $?"Each error names the file and line, prints the line with a ^ under the problem, and then the error type and message.
ReferenceError: consol is not definedmeans you used a name that does not exist. Check the spelling, and remember that JavaScript is case-sensitive:Console.logandconsole.Logfail the same way.SyntaxErrorwith something likeInvalid or unexpected tokenmeans the file is not valid JavaScript, so nothing in it ran. Look for a missing quote, bracket or parenthesis on or just before the line shown.ReferenceError: document is not defined(orwindow is not defined) means browser code is running in Node.js, as in the earlier step.SyntaxError: Cannot use import statement outside a modulemeans the nearestpackage.jsondoes not say"type": "module".- Typing
node hello.jsafter you have already startednodeon its own gives aSyntaxErrortoo. A prompt that starts with>is the Node.js REPL, which expects JavaScript, not commands. PressCtrl+D(orCtrl+Ctwice) to leave it, then run the command again.
Check it worked
The first command shows ReferenceError: consol is not defined and exit status 1. The second shows a SyntaxError pointing at quote.js line 1 and exit status 1.
When you have finished, delete the demo folder:
cd ~
rm -rf ~/hello-js-demoNext steps
- Learn how
letandconstdiffer, and when to use each: declare variables with let and const in JavaScript. - Add types to the same kind of program and catch mistakes before it runs: Hello World in TypeScript.
- On an Ubuntu server, install Node.js so apt keeps it patched: install Node.js 26 LTS on Ubuntu.
Related tutorials
- Hello World in Zig: zig run, zig build and zig ccYou will install a signature-checked Zig 0.17.1 on Ubuntu 26.04, write and run Hello World with the current I/O API, build a small project with zig build that handles errors explicitly, and use zig cc to compile a C program for Linux and for Windows.
- Hello World in x86-64 assembly with NASM and Linux system callsYou will write an x86-64 assembly program that prints a line using only Linux system calls, assemble it with NASM, link it with ld, check its exit status, then loop it three times and read its machine code with objdump.
- Hello World in WebAssembly text format with wabt and WasmtimeYou will write a WebAssembly module by hand in the text format, assemble it with wat2wasm, print Hello World through WASI in Wasmtime 50, and call an exported add function from both Wasmtime and Node.js.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials