Nodejs Interview Questions

Q1: What is Event Loop?

An event loop is just like a while loop. It executes one command at a time and it will run until Node has executed 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 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, milisecond);

setTimeout():- this function is called when delay time has finished. it is not executed immediately.

            let data=()=>{
            setTimeout(() => {
                console.log('set Timeout Call');
            }, 0);
                console.log("set Immediate Call")
                console.log('next Tick call');
Output:- Hello
next Tick call
set Immediate Call
set Timeout Call

Q3: What is Callback()?

A function pass as a argument to another function is called 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 it’s resulting value will be success or failure.
A Promise has basically three-state.
1) pending
2) fulfilled
3) rejected

Q5: What is REPL?

REPL stands for Read Eval Print Loop. It basically represents a computer environment like Linux/Unix terminal, window console, etc.

Q6: How will you debug an application in Node.js?

node --inspect filename(index.js)

Q7: What is package.json?

package.json file contain all npm packages which is used in the project. it is in project root folder. this has various metadata information relevant to the project like project name, version, description, package information.

Q8: How to include module in Nodejs?

to include module in nodejs through 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 basically use to handle a large amount of data. Stream workflow “read from input and write to output in sequentially manner” readmore…

Q13: What is EventEmitter?

Nodejs follow Event-Driven based architecture so every action is an event in Nodejs like open 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 eventEmitter = new events.EventEmitter();


Q14: Explain the concept of middleware in Node.js?

Middleware functions are functions that 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 needs to be declared before awaiting a function returning a Promise.

Q16: Explain 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
1) yield method
2) next method

Q17: What is Nodejs?

Ans:- Node.js is an open-source server environment and it uses asynchronous programming.

Q18: Why we use Nodejs?

Ans:- Node.js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It’s used for traditional web sites and back-end API services and real-time applications.

Q19: Who is creator of Node.js?

Ans:- Ryan Dahl

Q20: Advantage of Node.js

Ans:- Node. js offers many benefits for web app development like easy scalability, easy to learn, high performance, highly extensible, support of the large and active community.

Q21: Disadvantage of Node.js

Ans:- 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
a) Asynchronous, non-blocking functions
b) Synchronous, blocking functions

Q25: What is the use of Underscore variable in REPL?

_ symbol returns the result of the last logged expression in REPL node console.

Q26: What are local installations and global installation of dependencies?

local installations:-
when you use below command

npm install package-name

then save it, in your project node_modules directory.

global installation:-
when you use g in the below command.

npm install -g package-name

then save it,in your global node_modules directory and global node_modules directory situated where you installed Nodejs.

When we use

npm install package-name --save

then package name installs in the dependencies object which is in the package.json file.

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 callback after 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 require keyword.

var buf = Buffer.from('abc');

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.

  .css('background', 'green')

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 is 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 global variable and how to create this in Nodejs?

global variable means you can access this variable any where in the project.
you can create global variable through global object.


Now, you can get this variable


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

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?


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 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('').listen(server); 

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 basically used to fast execution.