JavaScript & Node.
js Interview Questions and Answers
JavaScript Interview Questions - Fundamentals
1. Difference between var, let, and const:
- var: Function-scoped, can be redeclared, hoisted (initialized as undefined).
- let: Block-scoped, cannot be redeclared in the same scope, hoisted but in Temporal Dead Zone.
- const: Block-scoped, cannot be reassigned or redeclared, hoisted but in Temporal Dead Zone.
2. Hoisting:
Hoisting moves declarations to the top of scope before execution. var is hoisted as undefined, functions are
hoisted with their definitions.
3. Event Loop:
JavaScript uses an event loop to handle async code. Microtasks (Promises) run before Macrotasks
(setTimeout).
4. Closures:
Function that remembers variables from its scope even when called outside.
5. 'this' keyword:
Refers to the object calling the function. Arrow functions inherit 'this' from parent scope.
6. == vs ===:
== checks value after type coercion, === checks value and type.
7. Prototypal Inheritance:
Objects inherit from other objects via prototype chain.
8. Promises:
Used for async tasks, avoids callback hell.
9. call(), apply(), bind():
call - invoke with arguments, apply - invoke with array args, bind - returns new bound function.
10. Higher-Order Functions:
Takes functions as arguments or returns them.
JavaScript Interview Questions - Advanced Concepts
1. Memoization:
Cache results to optimize performance.
2. IIFE:
Immediately invoked function expressions execute instantly.
3. Strict Mode:
Enforces stricter parsing and error handling.
4. Scope & Scope Chain:
Variables are looked up in nested scopes until found or global is reached.
JavaScript & Node.js Interview Questions and Answers
Node.js Interview Questions - Core Concepts
1. What is Node.js?
JS runtime environment built on Chrome V8.
2. Single-threaded Nature:
Node uses single-threaded event loop with libuv for async I/O.
3. Event Loop:
Handles async callbacks.
4. Streams:
Handle data in chunks. Types: Readable, Writable, Duplex, Transform.
5. Middleware in Express.js:
Functions processing requests before final handler.
6. NPM:
Package manager for Node.js.
7. exports vs module.exports:
module.exports is the actual export, exports is a reference.
8. package.json:
Metadata + dependencies.
9. Error Handling:
Use try/catch or error middleware.
Node.js - Asynchronous Programming & Performance
1. async/await:
Cleaner async handling.
2. Worker Threads:
Used for CPU-intensive tasks.
3. Clustering:
Run multiple processes for multi-core usage.
4. Prevent Callback Hell:
Use Promises or async/await.
5. setImmediate() vs process.nextTick():
nextTick runs before next event loop tick.
Node.js - Modules & Tools
JavaScript & Node.js Interview Questions and Answers
1. fs module:
File operations.
2. Buffer:
Binary data handling.
3. child_process:
Spawn and manage other processes.
4. __dirname / __filename:
Path of current module.
5. Debugging:
Use node --inspect or debugger.
Node.js - Security & Best Practices
1. Validate input to prevent injections.
2. Store secrets in environment variables (dotenv).
3. Protect against XSS, CSRF.
4. Use Helmet.js and rate limiting.