Top Node.js Interview Questions

1) What is Node.js and why it is used for ?
-> Node.js is a run time JavaScript environment built on top of Chrome’s V8 engine. It uses an event-driven, non-blocking I/O model. Node.js package system is called NPM.

2) What is event driven programming ?
-> Event driven programming is building an application based on and respond to events. When an event occurs like click or key-press a call back function is run which is registered to the element of that event.
-> Event driven programming follows mainly a publish-subscribe pattern.

3) What is Event Loop in Node.js and How does it work ?
->The Event loop handles all async callbacks. Node.js is a single threaded, event driven language. This means we can attach listeners to events and provide the callback. Whenever we call settimeout, http.get and fs.readfile, Node.js runs the operations and further continue to run subsequent code without waiting for the output. When async operations are finished, it receives and output and runs the callback function.
-> The callback functions are queued in a loop and will run one-by-one when the response has been received.

4) What is the purpose of module.js in Node.js ?
-> A module encapsulates related code into a single unit of code.

5) 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 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.

6) What is the difference between process.nextTick() and setImmediate() ?
-> process.nextTick() -> defers the execution of an action till the next pass around the event loop or its simply calls the callback function once the ongoing execution of the event loop is finished.
-> setImmediate() -> executes a callback on the next cycle of the event loop and it gives back to the event loop for executing any I/O operations.

 

Leave a Reply

Your email address will not be published. Required fields are marked *