NodeJs Interview Questions Part 1

1. What is Functional Programming?

In most simple terms, Functional Programming is a form of programming in which you can pass functions as parameters to other functions and also return them as values. In functional programming, we think and code in terms of functions.

2. What are first-class functions in JS?

In NodeJS, functions are treated like other variables. So the functions can be assigned to any other variable or passed as an argument or can be returned by another function.

3. What are the different states of Promise and what are the advantages of Promises over Callbacks?

  1. pending: Promise is still pending i.e. not fulfilled or rejected yet
  2. fulfilled: Action related to the promise succeeded
  3. rejected: Action related to the promise failed
  4. settled: Promise has been fulfilled or rejected
  • The main advantage of using promise is you get an object to decide the action that needs to be taken after the async task completes. This gives more manageable code and avoids callback hell.
  • They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events.

4.What are some commonly used timing features of Node.JS

  • setTimeout/clearTimeout â€” This is used to implement delays in code execution.
  • setInterval/clearInterval â€” This is used to run a code block multiple times.
  • setImmediate/clearImmediate â€” Any function passed as the setImmediate() argument is a callback that’s executed in the next iteration of the event loop.
  • process.nextTick â€” Both setImmediate and process.nextTick appear to be doing the same thing; however, you may prefer one over the other depending on your callback’s urgency

5. Why is Node.js single-threaded?

Node JS follows the Single-Threaded with Event Loop Model. Node JS Processing model mainly inspired by JavaScript Event-based model with JavaScript callback mechanism. Because of this Node.js can handle more concurrent client requests with ease. The event loop is the heart of the Node.js processing model as shown below diagram.

6. What is Event-Loop in Node JS?

Asynchronous code is managed by an event loop using a queue and listener. We can get the idea using the following diagram:

So when an async function needs to be executed(or I/O) the main thread sends it to a different thread allowing v8 to keep executing the main code. The event loop involves different phases with specific tasks such as timers, pending callbacks, idle or prepare, poll, check, and close callbacks with different FIFO queues. Also in between iterations it checks for async I/O or timers and shuts down cleanly if there aren’t any.

7. How does Node.js handle concurrency if it’s single-threaded?

The reason why node js is popular despite being single-threaded is the asynchronous nature that makes it possible to handle concurrency and perform multiple I/O operations at the same time. Node.js uses an event loop to maintain concurrency and perform non-blocking I/O operations.

8. What are Worker threads and why do we need them?

Worker Threads in Node.js is useful for performing heavy JavaScript tasks. With the help of threads, Worker makes it easy to run javascript codes in parallel making it much faster and more efficient. We can do heavy tasks without even disturbing the main thread.