Labs ICT
โญ Pro Login

Your First Node.js Program

Write and run a Node.js script.

Your First Node.js Program

You have Node.js installed. Let us write something real.

Try it Yourself โ†’

Running JavaScript in Node.js

Create a file called app.js:

// This is regular JavaScript
const name = "Alice";
const age = 25;
console.log(`Hello, ${name}! You are ${age} years old.`);

// Do some math
const sum = (a, b) => a + b;
console.log("2 + 3 =", sum(2, 3));

Run it:

node app.js

You will see the output in your terminal. This is the same JavaScript you know from the browser โ€” but running on your machine.

Node.js Has No Window

One key difference: there is no window object in Node.js. The browser gives you window.document, window.location, and all those browser APIs. Node.js does not have those.

Instead, Node.js gives you global objects like process, Buffer, and __dirname.

// Access command-line arguments
console.log("Arguments:", process.argv);

// Get current directory
console.log("Directory:", process.cwd());

// Environment variables
console.log("Node version:", process.version);

The REPL

Node.js has a Read-Eval-Print Loop โ€” an interactive console. Just type node without any arguments:

$ node
> 2 + 2
4
> "hello".toUpperCase()
'HELLO'
> .exit

This is great for quick experiments and testing code snippets.

Key Takeaway: Node.js is just JavaScript. If you know JavaScript, you already know 80% of Node.js. The new parts are the modules (fs, http, path) and the async patterns.

๐Ÿงช Quick Quiz

What does process.argv contain?