Node.js First Program Examples

Last Updated : 9 Aug 2026

Node.js is used to execute JavaScript code outside of a web browser and helps to build server-side applications. Let us explore a few Node.js examples.

Example 1: Print Message using console.log()

Below is a very simple example that prints a message using console.log().

console.log('Hello Tpoint Tech');

Output:

Hello Tpoint Tech

Explanation:

The console.log() function simply displays a message in the console.

Example 2: Perform Arithmetic Operation in Node.js

Let us look at another example where we will perform the addition operation.

Code:

const firstNumber = 20;
const secondNumber = 30;
const addition = firstNumber + secondNumber;
console.log(addition); 

Output:

50

Explanation:

We have taken two variables firstNumber and secondNumber. The variable firstNumber stores the value 20 and the variable secondNumber stores the value 30. We have added both the numbers and stored the result in the variable called addition then printed the addition of both the numbers.

Example 3: Working with Variables

We will look at an example in which we learn how to declare variables and display their values in Node.js. Variables are used to store data that can be accessed and utilized throughout a program.

Code:

const name = "John";
const age = 25;
console.log("Name:", name);
console.log("Age:", age);

Output:

Name: John
Age: 25

Explanation:

We have declared two variables named name and age using the const keyword. The variable name stores the value "John" while the variable age stores the value 25. The console.log() function is then used to display both values in the console.

Example 4: Using Conditional Statements

We will utilize an if-else statement for checking whether a student has passed or failed on the basis of their marks.

Code:

const marks = 85;
if (marks >= 40) {
    console.log("Pass");
} else {
    console.log("Fail");
}

Output:

Pass

Explanation:

We have a variable called marks that stores the value 85. The if statement checks whether the marks are greater than or equal to 40. Since the condition evaluates to true so the message "Pass" is displayed. If the marks are less than 40 then the else block would execute and display "Fail".

Example 5: Creating and Calling a Function

This example shows how functions can be utilized to organize and reuse code in Node.js applications. We will create a simple function that accepts a name as a parameter and returns a greeting message.

Code:

function greet(name) {
    return `Hello, ${name}!`;
}
console.log(greet("John"));

Output:

Hello, John!

Explanation:

Here, a function named greet() is created with a parameter called name. When the function is called with the value "John" then the function returns the greeting message "Hello, John!".

Example 6: Create a File using the File System Module

We will create a file in this example and write content to it using the fs.writeFile() method.

Code:

const fs = require('fs');
fs.writeFile('sample.txt', 'Welcome to Tpoint Tech', (err) => {
    if (err) {
        console.log(err);
        return;
    }

    console.log('File created successfully.');
});

Output:

File created successfully.

Explanation:

We have imported the fs module using the require() function. Here, the writeFile() method creates a file named sample.txt and writes the text "Welcome to Tpoint Tech" into it. If an error occurs during the operation then it is displayed on the console. Otherwise the message "File created successfully" is printed on the console.

Example 7: Get the Current File Name Using the Path Module

Let us look at an example that displays the name of the current file utilizing the path module.

File: index.js

const path = require('path');
console.log(path.basename(__filename));

Output:

index.js

Explanation:

The path module is imported using the require() function. The __filename variable contains the complete path of the currently executing file. The basename() method extracts only the file name from that path and returns index.js which is then displayed on the console.

Example 8: Retrieve Operating System Information

We will retrieve information about the operating system and the system architecture in the following example.

Code:

const os = require('os');
console.log(os.platform());
console.log(os.arch());

Output:

win32
x64

Explanation:

The os module is imported using the require() function. The platform() method returns the operating system platform while the arch() method returns the system architecture.


Next TopicNode.js Console