Most popular 21 Node.js interview questions with answers

1. What is Node.js?

Wikipedia defines Node.js as “an open-source, cross-platform runtime environment for developing server-side Web applications.” It is essentially server side scripting which is used to build scalable programs.

Features of Node JS

Features Description
Fast Node.js is built on Google Chrome’s V8 JavaScript Engine which makes its library very fast in code execution
Asynchronous Node.js based server never waits for an API to return data thus making it asynchronous
 Scalable It is highly scalable because of its event mechanism which helps the server to respond in a non-blocking way
Open Source Node.js has an extensive open source community which has contributed in producing some excellent modules to add additional capabilities to Node.js applications
No Buffering Node.js applications simply output the data in chunks and never buffer any data.

2. What is the relation of Node.js with JavaScript? 

  • Though Node.js is not a JavaScript framework, many of its modules are written in JavaScript.
  • It allows the developers to create new modules in JavaScript.
  • Node.js is a virtual machine that leverages JavaScript as its scripting language to achieve high output.

3. What is the fundamental difference between Node.js and Ajax?

While Ajax is a client-side technology, Node.js is a server-side JavaScript environment.

4. Explain the term I/O in the context of Node.js.

I/O stands for input and output. It is used to access anything outside of the application. I/O gets loaded on to the machine memory in order to run programs after the application is fired up.

5. Why is Node.js single threaded?

For asynchronous processing, Node.js was created in a detailed manner as an experiment. More performance and scalability is possible by doing asynchronous processing on single thread under some typical web loads.

 

6. What are Promises?

Promises are a concurrency primitive, first described in the 80s. Now they are part of most modern programming languages to make your life easier. Promises can help you better handle async operations.

An example can be the following snippet, which after 100ms prints out the result string to the standard output. Also, note the catch, which can be used for error handling. Promises are chainable.

new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('result')
  }, 100)
})
  .then(console.log)
  .catch(console.error)

 

7. When are background/worker processes useful? How can you handle worker tasks?

Worker processes are extremely useful if you’d like to do data processing in the background, like sending out emails or processing images.

There are lots of options for this like RabbitMQ or Kafka.

8. What is meant by chaining in node.js?

It is a procedure where the output of one stream is connected to another stream which in turn creates a chain of multiple stream operations.

 

9. What is meant by callback in node.js?

This function is called as the completion of given task. It allows other code to run in the meantime and also it prevents any kind of blocking. Being an async platform, node.js rely on the callback a lot. All of the API in Node are written in a way so that it will support callbacks.

 

10. What is an error-first callback?

Error-first callbacks are used to pass errors and data as well. You have to pass the error as the first parameter, and it has to be checked to see if something went wrong. Additional arguments are used to pass data.

fs.readFile(filePath, function(err, data) {  
  if (err) {
    // handle the error, the return is important here
    // so execution stops here
    return console.log(err)
  }
  // use the data object
  console.log(data)
})

11. How can you avoid callback hells?

There are lots of ways to solve the issue of callback hells:

  • modularization: break callbacks into independent functions
  • use a control flow library, like async
  • use generators with Promises

12: What are the timing features of Node.js?

The Timers module in Node.js contains functions that execute code after a set period of time.

  • setTimeout/clearTimeout – can be used to schedule code execution after a designated amount of milliseconds
  • setInterval/clearInterval – can be used to execute a block of code multiple times
  • setImmediate/clearImmediate – will execute code at the end of the current event loop cycle
  • process.nextTick – used to schedule a callback function to be invoked in the next iteration of the Event Loop
function cb(){
  console.log('Processed in next iteration');
}
process.nextTick(cb);
console.log('Processed in the first iteration');

Output:

Processed in the first iteration
Processed in next iteration

🔗Source: github.com/jimuyouyou

 

13: Explain what is Reactor Pattern in Node.js?

Reactor Pattern is an idea of non-blocking I/O operations in Node.js. This pattern provides a handler(in case of Node.js, a callback function) that is associated with each I/O operation. When an I/O request is generated, it is submitted to a demultiplexer.

This demultiplexer is a notification interface that is used to handle concurrency in non-blocking I/O mode and collects every request in form of an event and queues each event in a queue. Thus, the demultiplexer provides the Event Queue.

At the same time, there is an Event Loop which iterates over the items in the Event Queue. Every event has a callback function associated with it, and that callback function is invoked when the Event Loop iterates.

🔗Source: hackernoon.com

14: What is LTS releases of Node.js why should you care?

An LTS(Long Term Support) version of Node.js receives all the critical bug fixes, security updates and performance improvements.

LTS versions of Node.js are supported for at least 18 months and are indicated by even version numbers (e.g. 4, 6, 8). They’re best for production since the LTS release line is focussed on stability and security, whereas the Current release line has a shorter lifespan and more frequent updates to the code. Changes to LTS versions are limited to bug fixes for stability, security updates, possible npm updates, documentation updates and certain performance improvements that can be demonstrated to not break existing applications.

🔗Source: github.com/i0natan/nodebestpractices

 

15. Explain ECMAScript ?

ECMAScript is the standard on which Javascript is based on. It was created to standardize Javascript. It is commonly used for client-side scripting on the World Wide Web and used by Node Js for writing server applications and services.

 

16. What are events ?

An event is an action or occurrence recognized by software/app that is handled by event handler by writing a code that will be executed when the event fired.
Mouse move, Click, file copied or deleted are some examples of events.
In Node Js there are two types of events.
1)System Events: The event that comes from the C++ side.
2)Custom Events: Custom events are user-defined events.

 

17. What does it mean “non-blocking” in node.js?

In node.js “non-blocking” means that its IO is non-blocking.  Node uses “libuv” to handle its IO in a platform-agnostic way. On windows, it uses completion ports for unix it uses epoll or kqueue etc. So, it makes a non-blocking request and upon a request, it queues it within the event loop which call the JavaScript ‘callback’ on the main JavaScript thread.

 

18. What is the command that is used in node.js to import external libraries?

Command “require” is used for importing external libraries, for example, “var http=require (“http”)”.  This will load the http library and the single exported object through the http variable.

 

19. List out some REPL commands in Node.js?

Below are the list of REPL commands –

  • Ctrl + c – For terminating the current command.
  • Ctrl + c twice – For terminating REPL.
  • Ctrl + d – For terminating REPL.
  • Tab Keys – list of all the current commands.
  • .break – exit from multiline expression.
  • .save with filename – save REPL session to a file.

 

20. Mention the command to stop REPL in Node.js?

Command – ctrl + c twice is used to stop REPL.

 

21. Explain NPM in Node.js?

NPM stands for Node Package Manager (npm) and there are two functionalities which NPM takes care of mainly and they are –

  • Online repositories for node.js modules or packages, which can be searched on search.nodejs.org
  • Dependency Management, Version Management and command line utility for installing Node.js packages.