Node JS Interview Questions and Answers

What is Node Js ?

Node.js is a very powerful JavaScript based platform or framework which is built on Google Chrome's JavaScript V8 Engine.

What are the features of Node.js?

Here is the main features and advantages of node js is,

  • Asynchronous and Event Driven
  • Very Fast
  • Single Threaded but Handling multiple threads at a time.
  • No Buffering

 

Callbacks in Node Js.

Callback is a asynchronous function. It will execute in asynchronous manner. In node js heavily used callback functions for non blocking. And also execute parallel.

Callback function example : 

function sum(a,b,callback){
    callback(a+b);
}

sum(2,3,function(result){
    console.log(result);
});

 

 

 

What is callback hell in Node.js?

Callback hell is nothing but writing heavily nested Callback functions. It is very hard to maintain, unreadable and error handling also somewhat difficult.
Example :
function sum(a,b,callback){
    callback(a+b);
}
 
sum(2,3,function(result){
	console.log(result);
	sum(6,9,function(result1){
		console.log(result1);
		sum(12,18,function(result2){
			console.log(result2);
			sum(24,36,function(result3){
				console.log(result3);
				sum(48,72,function(result3){
					console.log(result3);
				});
			});
		});
	});
});

How to avoid Callback Hell in Node.js

By using Promises we can avoid callback hell
Example for Promises

function sum(a,b){
    return new Promise(function(resolve, reject) {
        if(isNaN(a) || isNaN(b)){
            reject("values must be integers");
        }else{
            resolve(parseInt(a)+parseInt(b));
        }
    });
}

sum(2,'3')
.then(function(result){console.log(result);})
.catch(function(error){console.log(error);});

Here success response will get in then function, failure response will get in catch function

Below are the some modules for avoiding callback hell.
promise, async, q, deferred

when to use Node.js?

Node.js can be used for the following type of applications

a) Web applications
b) Light Weight applications (because of single threaded)
c) Distributed systems
d) Gaming applications
e) Chat applications

How to access DOM using Node.js?

No we can't  access DOM from the node, because node don't have DOM/window objects.

What is the difference between Node.js vs Ajax?

The only similarity is both are javascript. But both are doing different works.

Ajax is client side technology, without reaload the page we can do some operations are loading content and saving information. Comming to Node js is server side technology. Any db related works we can do with node js and Node.js does not work in the browser. 

 

Which framework most used in node.js?

Express is the most used framework in node.js because Fast and flexible for all different kinds of requirements. And also Express is the part of MEAN stack

List of some Node.js Global Objects


Global Objects nothing but no need include in your application, we can directly access and use them.

Below are the some global objects
__dirname
__filename
process
buffer

In general
console
timeout functions like setTimeout/setInterval/setImmediate