JavaScript & Node.
js Interview Questions and Answers
Section 1: JavaScript Questions
Q1: What is the difference between **, ** , and ``?\ A:
• var is function-scoped and can be re-declared.
• let is block-scoped and can be updated but not re-declared in the same scope.
• const is also block-scoped but cannot be updated or re-declared.
Q2: Explain closures in JavaScript.\ A: A closure is a function that retains access to its outer scope, even
after the outer function has finished executing.
Q3: What is event delegation?\ A: Event delegation is a technique where a parent element handles events
for its child elements using event bubbling.
Q4: What are promises and how are they used?\ A: Promises are used to handle asynchronous
operations. They have three states: pending, fulfilled, and rejected. You can chain .then() and
.catch() for handling results and errors.
Q5: What is the difference between ** and ** ?\ A: == compares values with type coercion. ===
compares both value and type strictly.
Q6: Explain `** keyword in JavaScript.**\
**A:** this` refers to the context in which a function is called. It varies depending on how the function is
invoked.
Q7: What is the difference between synchronous and asynchronous code?\ A: Synchronous code runs
sequentially, while asynchronous code allows operations to run in the background, improving performance.
Section 2: Node.js Questions
Q1: What is Node.js?\ A: Node.js is a runtime environment that lets you run JavaScript on the server side
using the V8 engine.
Q2: What is the difference between CommonJS and ES Modules?\ A: CommonJS uses require() and
module.exports , while ES Modules use import and export . Node now supports both, but ES
Modules require .mjs or specific settings.
Q3: What are streams in Node.js?\ A: Streams are objects that let you read or write data continuously.
Types: Readable, Writable, Duplex, and Transform.
Q4: What is the role of ``?\ A: It manages project metadata, dependencies, scripts, and configurations for a
Node.js application.
1
Q5: What is middleware in Express.js?\ A: Middleware are functions that run before the final request
handler. They have access to req , res , and next .
Q6: How does the event loop work in Node.js?\ A: The event loop handles non-blocking I/O operations by
offloading operations to the system kernel whenever possible.
Q7: How do you handle exceptions in async/await code?\ A: Use try...catch blocks to handle
exceptions in asynchronous functions using async/await.
Section 3: JavaScript & Node.js Coding Questions
Q1: Reverse a string in JavaScript
function reverseString(str) {
return str.split('').reverse().join('');
}
console.log(reverseString("hello")); // "olleh"
Q2: Check if a string is a palindrome
function isPalindrome(str) {
const reversed = str.split('').reverse().join('');
return str === reversed;
}
console.log(isPalindrome("madam")); // true
Q3: Find the largest number in an array
function findMax(arr) {
return Math.max(...arr);
}
console.log(findMax([1, 4, 9, 2])); // 9
Q4: Create a simple server in Node.js using HTTP module
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, Node.js!');
});
2
server.listen(3000, () => {
console.log('Server running on port 3000');
});
Q5: Read a file using Node.js
const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data);
});
Section 4: React.js Interview Questions
Q1: What is React?\ A: React is a JavaScript library for building user interfaces, primarily for single-page
applications. It uses a virtual DOM to efficiently update the UI.
Q2: What are components in React?\ A: Components are reusable pieces of UI. There are two types:
functional components and class components.
Q3: What is JSX?\ A: JSX stands for JavaScript XML. It allows you to write HTML-like code in your JavaScript
files.
Q4: What is the difference between props and state?\ A: Props are read-only and passed from parent to
child. State is managed within the component and can be changed using useState or this.setState .
Q5: What is useEffect?\ A: useEffect is a React hook that allows you to perform side effects in function
components, such as data fetching or DOM manipulation.
Q6: What is the virtual DOM?\ A: The virtual DOM is a lightweight copy of the real DOM that React uses to
optimize updates by diffing and batch rendering.
Q7: How does React handle form input?\ A: React uses controlled components, where form inputs are
tied to state using useState , and updates are handled via onChange handlers.