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
console.log()is used to display information.- The text
"Hello, Node.js!"is passed toconsole.log(). - Node.js executes the JavaScript code.
- 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
- Open Command Prompt, PowerShell, or Terminal.
- Type
node --version. - Press Enter.
- 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
- Create a file called
welcome.js. - Add JavaScript code to the file.
- Open the terminal in the same folder.
- Run
node welcome.js. - Node.js executes the JavaScript file.
- 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
firstNumberstores20.secondNumberstores30.- The
+operator adds both numbers. - The result is stored in
result. 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
let namecreates a variable calledname."Rahul"is stored inside the variable.let agecreates a variable calledage.18is stored insideage.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
- Three variables are created.
namestores the person’s name.agestores the person’s age.citystores the person’s city.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
function welcome()creates a function namedwelcome.- The code inside
{ }belongs to the function. console.log()displays the message.welcome()calls the function.- 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
process.argvcontains command-line arguments.process.argv[2]gets the first value provided by the user.- In this example,
Rishabhis provided after the filename. - The value is stored in
name. - The program joins the greeting with the name.
- 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
- The variable
numberstores12. %is the remainder operator.12 % 2gives0.- The
ifcondition checks whether the remainder is0. - Because the condition is true, the program prints
"The number is even.". - If the remainder were not
0, theelseblock 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
require("http")loads Node.js’s built-in HTTP module.http.createServer()creates an HTTP server.- The
requestobject contains information about the incoming request. - The
responseobject is used to send a response. response.writeHead(200, ...)sends an HTTP success status.response.end()sends the message to the browser.server.listen(3000)starts the server on port3000.- 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
nodecommand. node --versionchecks the installed Node.js version.console.log()is useful for displaying output.- Node.js supports variables, functions, conditions, and calculations.
process.argvcan be used to access command-line arguments.- Node.js includes useful built-in modules.
- The
httpmodule 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.
