Node.js Introduction Practice Questions with Solutions

Introduction

Node.js is a JavaScript runtime that allows developers to run JavaScript outside the browser. It is commonly used to build web servers, APIs, command-line tools, and backend applications. In this chapter, you will practice the basic concepts of Node.js through 10 simple solved questions. These examples start from the fundamentals and gradually help you understand how Node.js works in real development. Node.js Introduction practice questions with solutions help to build concepts.

Question 1: What is Node.js?

Problem

Write a simple Node.js program that displays a message on the terminal.

Solution

console.log("Hello, Node.js!");

Output

Hello, Node.js!

Step-by-Step Explanation

  1. console.log() is used to display information.
  2. The text "Hello, Node.js!" is passed to console.log().
  3. Node.js executes the JavaScript code.
  4. The message appears in the terminal.

To run the program, save it as app.js and use:

node app.js

Question 2: How do you check the Node.js version?

Problem

Use the terminal to check which version of Node.js is installed on your computer.

Solution

node --version

Output

Your output will look similar to:

v22.x.x

The exact version depends on the Node.js version installed on your computer.

Step-by-Step Explanation

  1. Open Command Prompt, PowerShell, or Terminal.
  2. Type node --version.
  3. Press Enter.
  4. Node.js displays the installed version.

You can also use:

node -v

Both commands show the Node.js version.


Question 3: How do you run a JavaScript file using Node.js?

Problem

Create a JavaScript file that prints a welcome message and run it using Node.js.

Solution

Create a file named welcome.js:

console.log("Welcome to Node.js learning!");

Run it from the terminal:

node welcome.js

Output

Welcome to Node.js learning!

Step-by-Step Explanation

  1. Create a file called welcome.js.
  2. Add JavaScript code to the file.
  3. Open the terminal in the same folder.
  4. Run node welcome.js.
  5. Node.js executes the JavaScript file.
  6. The result appears in the terminal.

This is one of the first things you should learn when starting Node.js.


Question 4: Can Node.js perform calculations?

Problem

Create a Node.js program that adds two numbers.

Solution

let firstNumber = 20;
let secondNumber = 30;

let result = firstNumber + secondNumber;

console.log(result);

Output

50

Step-by-Step Explanation

  1. firstNumber stores 20.
  2. secondNumber stores 30.
  3. The + operator adds both numbers.
  4. The result is stored in result.
  5. console.log() displays the result.

Node.js can execute normal JavaScript operations just like JavaScript running in a browser.


Question 5: How can you store information in variables in Node.js?

Problem

Create variables for a person’s name and age and display them.

Solution

let name = "Rahul";
let age = 18;

console.log(name);
console.log(age);

Output

Rahul
18

Step-by-Step Explanation

  1. let name creates a variable called name.
  2. "Rahul" is stored inside the variable.
  3. let age creates a variable called age.
  4. 18 is stored inside age.
  5. console.log() displays both values.

Node.js supports JavaScript variables such as let, const, and var.


Question 6: How can you display multiple values using Node.js?

Problem

Create a Node.js program that displays a person’s name, age, and city.

Solution

let name = "Aman";
let age = 20;
let city = "Delhi";

console.log("Name:", name);
console.log("Age:", age);
console.log("City:", city);

Output

Name: Aman
Age: 20
City: Delhi

Step-by-Step Explanation

  1. Three variables are created.
  2. name stores the person’s name.
  3. age stores the person’s age.
  4. city stores the person’s city.
  5. console.log() displays each value.

This type of output is useful when testing your Node.js programs.


Question 7: How can you use a function in Node.js?

Problem

Create a function that displays a welcome message.

Solution

function welcome() {
    console.log("Welcome to Node.js!");
}

welcome();

Output

Welcome to Node.js!

Step-by-Step Explanation

  1. function welcome() creates a function named welcome.
  2. The code inside { } belongs to the function.
  3. console.log() displays the message.
  4. welcome() calls the function.
  5. Node.js executes the function.

Functions are an important part of Node.js because Node.js applications contain many reusable functions.


Question 8: How can you create a simple Node.js greeting program?

Problem

Create a program that accepts a name from the command line and displays a greeting.

Solution

let name = process.argv[2];

console.log("Hello, " + name + "!");

Run the program:

node greeting.js Rishabh

Output

Hello, Rishabh!

Step-by-Step Explanation

  1. process.argv contains command-line arguments.
  2. process.argv[2] gets the first value provided by the user.
  3. In this example, Rishabh is provided after the filename.
  4. The value is stored in name.
  5. The program joins the greeting with the name.
  6. Node.js prints the final message.

This is a simple introduction to taking input from the command line.


Question 9: How can you use Node.js to check whether a number is even or odd?

Problem

Write a Node.js program that checks whether a number is even or odd.

Solution

let number = 12;

if (number % 2 === 0) {
    console.log("The number is even.");
} else {
    console.log("The number is odd.");
}

Output

The number is even.

Step-by-Step Explanation

  1. The variable number stores 12.
  2. % is the remainder operator.
  3. 12 % 2 gives 0.
  4. The if condition checks whether the remainder is 0.
  5. Because the condition is true, the program prints "The number is even.".
  6. If the remainder were not 0, the else block would run.

Node.js supports JavaScript conditional statements just like browser-based JavaScript.


Question 10: How can you create a simple Node.js HTTP server?

Problem

Create a basic Node.js web server that displays "Hello from my Node.js server!" when someone visits it.

Solution

const http = require("http");

const server = http.createServer((request, response) => {
    response.writeHead(200, {
        "Content-Type": "text/plain"
    });

    response.end("Hello from my Node.js server!");
});

server.listen(3000, () => {
    console.log("Server is running on port 3000");
});

Run the file:

node server.js

Output in Terminal

Server is running on port 3000

Now open:

http://localhost:3000

You will see:

Hello from my Node.js server!

Step-by-Step Explanation

  1. require("http") loads Node.js’s built-in HTTP module.
  2. http.createServer() creates an HTTP server.
  3. The request object contains information about the incoming request.
  4. The response object is used to send a response.
  5. response.writeHead(200, ...) sends an HTTP success status.
  6. response.end() sends the message to the browser.
  7. server.listen(3000) starts the server on port 3000.
  8. You can access the server using http://localhost:3000.

This is your first step toward understanding how Node.js can be used for backend and web development.


Key Takeaways

  • Node.js allows JavaScript to run outside the browser.
  • Node.js is commonly used for backend development.
  • JavaScript files can be executed using the node command.
  • node --version checks the installed Node.js version.
  • console.log() is useful for displaying output.
  • Node.js supports variables, functions, conditions, and calculations.
  • process.argv can be used to access command-line arguments.
  • Node.js includes useful built-in modules.
  • The http module can be used to create a web server.
  • server.listen() starts a Node.js server on a specified port.
  • Node.js is built on JavaScript, so many JavaScript fundamentals are directly useful when learning Node.js.

FAQs

1. What is Node.js?

Node.js is a JavaScript runtime that allows JavaScript code to run outside a web browser. It is widely used for backend applications, APIs, servers, command-line tools, and other applications.

2. Is Node.js a programming language?

No. Node.js is not a programming language. JavaScript is the programming language, while Node.js is a runtime environment that allows JavaScript to run outside the browser.

3. Is Node.js difficult for beginners?

Node.js becomes much easier if you already understand basic JavaScript concepts such as variables, functions, conditions, arrays, and objects. Beginners can learn Node.js step by step with small programs.

4. What is Node.js mainly used for?

Node.js is commonly used for backend development, REST APIs, web servers, real-time applications, command-line applications, and applications that communicate with databases.

5. How do I run a Node.js file?

Open a terminal in the folder containing your JavaScript file and use the node command. For example:

node app.js

6. What is npm in Node.js?

npm stands for Node Package Manager. It is used to install, manage, and share JavaScript packages that can be used in Node.js projects.

7. Can Node.js create a website?

Yes. Node.js can be used to create the backend of a website. It can handle requests, send responses, work with databases, create APIs, and perform other server-side tasks.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top