Here are some common Node.js interview questions, we covered 41 Node.js interview questions.
Q1: What is Event Loop?
An event loop is just like a while loop. It executes one command at a time and will run until Node executes every line of code. It has
- One Process
- One Thread
- One Event Loop
Q2: Difference between setImmediate(), setInterval() and setTimeout()
setImmidate():- this function will execute code at the end of the current event loop cycle. setImmediate() will execute after any I/O operations in the current event loop and before any timers are scheduled (setInterval(), setTimeout()) for the next event loop.
setInterval():- this function is used to repeat the execution of your code block at specified intervals.
setInterval(callback, milliseconds);
setTimeout():- this function is called when the delay time has finished. it is not executed immediately.
console.log("Hello");
let data=()=>{
console.log("Hi");
}
setTimeout(() => {
console.log('set Timeout Call');
}, 0);
setImmediate(()=>{
console.log("set Immediate Call")
})
process.nextTick(()=>{
console.log('next Tick call');
})
data();
console.log("Bye");
Bye
Hi
next Tick call
set Immediate Call
set Timeout Call
Q3: What is Callback()?
A function passed as an argument to another function is called a callback function. In Nodejs Asynchronous operation is handled by callback function.
readmore…Q4: What is Promise?
The promise is used to handle async operation and its resulting value will be success or failure.
A Promise has three states.
- pending
- fulfilled
- rejected
Q5: What is REPL?
REPL stands for Read Eval Print Loop. It represents a computer environment like a Linux/Unix terminal, window console, etc.
readmore…Q6: How will you debug an application in Node.js?
node --inspect filename(index.js)
Q7: What is package.json?
package.json file contains all npm packages used in the project. it is in the project root folder. this has various metadata information relevant to the project like project name, version, description, and package information.
Q8: How to include the module in Nodejs?
to include the module in nodejs through the require() method with the name of the module.
var http = require('http');
Q9: What is the use export in Nodejs?
exports keyword is used to make properties and methods available outside the module file.
Q10: What is Express?
Express is a Nodejs Framework.
Q11: What is Authorization?
Authorization is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token.
Q12: What is stream?
The stream has the most important role in Nodejs. A stream is an object and use to handle a large amount of data. Stream workflow “read from the input and write to output in sequential manner”
readmore…Q13: What is EventEmitter?
Nodejs follows Event-Driven based architecture so every action is an event in Nodejs like opening the file to read is an event.
In Nodejs to use Event Emitter firstly include event module in the code.
var events = require('events');
after that create the object through a new keyword.
var events = require('events');
readmore…
Q14: Explain the concept of middleware in Node.js.
Middleware functions have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle.
Q15: What is Asynq await?
await function is used to return the Promise. But the function async must be declared before awaiting a function returning a Promise.
Q16: What is Generators?
Generators is a function that is used for lazy execution. it is used to execution suspended and resumed at a later point.
Generators have two methods
- yield method
- next method
Q17: What is Nodejs?
Node.js is an open-source server environment and it uses asynchronous programming.
Q18: Why we use Nodejs?
Node.js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It’s used for traditional websites, back-end API services, and real-time applications.
Q19: Who is the creator of Node.js?
Ryan Dahl
Q20: Advantage of Node.js
Node. js offers many benefits for web app development like easy scalability, easy to learn, high performance, high extensibility, and support of the large and active community.
Q21: Disadvantage of Node.js
Not efficient in handling CPU-intensive apps. Being an event-based and a single-threaded environment.
Q22: What is NPM?
NPM is a package manager for Node.js packages.
Q23: Which framework is most commonly used in Node.js?
express, Meteor,Koa,LoopBack.
Q24: What are the two types of API functions in Node.js?
The two types of API functions in Node.js are
- Asynchronous, non-blocking functions
- Synchronous, blocking functions
Q25: What is the use of the Underscore variable in REPL?
_ symbol returns the result of the last logged expression in the REPL node console.
Q26: What are local installations and global installations of dependencies?
local installations:- when you install a new package through the below command then save it, in your project node_modules directory.
npm install package-name
global installation:- when you install a new package through the below command and save it globally, the global node_modules directory is situated where you installed Nodejs.
npm install -g package-name
Where g represents global.
Q26: How to check the already installed dependencies using npm?
npm list --depth=0
Q27: How to uninstall the dependency using npm?
npm uninstall package-name --save
Q28: How to update the dependency using npm?
npm update package-name
Q29: What is callback Hell?
the concept of callbacks is great but if you need to make a callback after a callback then really confusing and difficult-to-read code.
Q30: What is Buffer Object in Nodejs?
The Buffer object is a global object in Node.js, and it is not necessary to import it using the required keyword.
var buf = Buffer.from('abc');
console.log(buf);
The Buffer class was introduced as part of the Node.js API to make it possible to manipulate or interact with streams of binary data.
Q31: In Node.js, which module is used for web-based operations?
HTTP module
Q32: What is Method Chaining in JavaScript?
Method chaining is a technique to simplify code in scenarios that involve performing multiple operations on the same object.
$('#my-data')
.css('background', 'green')
.height(100)
.fadeIn(400);
Q33: What is Promise chaining?
we have a sequence of asynchronous tasks to be performed one after another.
new Promise(function(resolve, reject) {
setTimeout(() => resolve(1), 1000); // (*)
}).then(function(result) { // (**)
alert(result); // 1
return result * 2;
}).then(function(result) { // (***)
alert(result); // 2
return result * 2;
}).then(function(result) {
alert(result); // 4
return result * 2;
});
Q34: Which module is used for file systems?
fs module is used.
Q35: What are the uses of __filename and __dirname variables in Node.js?
__filename tells you the absolute path of the filename where code being executed.
console.log( __filename ); //output:- /project/data/server.js
__dirname tells you the absolute path of the directory containing the currently executing file.
console.log(__dirname) //Output:- "/project/data/"
Q36: What is a global variable and how to create this in Nodejs?
global variable means you can access this variable anywhere in the project.
you can create a global variable through the global object.
global.variable_name='mydata';
Now, you can get this variable
console.log(variable_name);
Q37: How to get post/get/parameter Data in Node.js?
req.body is used to post data
req.query is used to get data
req.params is used to the parameter value through req.params like get the ids
http://abc.com/id/23
Q38: What are the fundamental differences between Node.js and Ajax?
Nodejs is a server-side javascript while Ajax is client-side javascript.
Q39: Which method is used to import external libraries in Node.js?
require()
Q40: Difference between app.listen() and server.listen()?
app.listen() returns the HTTP server instance.
if you want to reuse the HTTP server then it is not possible through app.listen() then we need server.listen().
Example:- to run socket.io within the same HTTP server instance then need server.listen()
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
server.listen(1234);
Q41: What is Promise.all()?
Promises.all() is used to collect all promises and rolls them up into a single promise. Once all of the inner promises resolve successfully then show successfully if any one promise rejects then Promise.all() show rejects. Promise.all() is used to fast execution.