Node.js is a powerful JavaScript runtime that allows you to run JavaScript code outside of a browser, typically on servers. It runs on Google’s V8 engine and follows a non-blocking, event-driven architecture, making it ideal for managing high volumes of simultaneous client requests without slowing down.
JavaScript Interview Questions
Prepare for your next interview with these carefully curated questions
1. What is Node.js, and how does it work?
Answer
Tips
- No specific tips available for this question
2. What is the difference between setImmediate() and process.nextTick()?
Answer
Using process.nextTick()
Node.js schedules a function to run right after the current operation completes, but before the event loop continues with new tasks.
setImmediate() executes the callback after the current event loop phase is completed.
Tips
- No specific tips available for this question
3. What is the difference between spawn, exec, and fork in Node.js?
Answer
spawn – Executes a command and returns a stream (useful for large outputs).
exec – It's great for executing commands and collecting their full response, but only if you're expecting a small result.
fork – Used to create a new child process with IPC (Inter-Process Communication).
Tips
- No specific tips available for this question
4. What is the event loop in Node.js?
Answer
The event loop handles asynchronous callbacks in Node. It allows Node to perform non-blocking I/O operations.
Tips
- Use diagrams or examples if asked further
5. Explain the concept of non-blocking I/O.
Answer
Non-blocking I/O means the server continues processing requests while I/O operations are happening in the background.
Tips
- Connect this to Node's performance
6. What are streams in Node.js?
Answer
Streams are objects for reading or writing data continuously. Types: Readable, Writable, Duplex, Transform.
Tips
- Mention types of streams
7. How does Node.js handle child processes?
Answer
Node uses the child_process
module to spawn and manage child processes for tasks like running shell commands.
Tips
- Mention modules
8. What are CommonJS and ES Modules?
Answer
CommonJS uses require()
, ES Modules use import/export
. Node.js supports both CommonJS and ES Modules, although ES Modules are the more modern standard introduced in recent versions.
Tips
- Know the syntax difference
9. What is the purpose of the package.json file?
Answer
It contains project info, dependencies, scripts, and configuration. It’s essential for managing any Node project.
Tips
- Talk about project metadata
10. Explain middleware in Express.js.
Answer
Middleware are functions that execute in the request-response cycle. Useful for auth, logging, body parsing, etc.
Tips
- Use real-world use cases
11. What is clustering in Node.js?
Answer
Clustering allows Node to spawn multiple worker processes to handle load across CPU cores for better performance.
Tips
- Mention CPU core usage