Untitled Document
Untitled Document
Developers
✅ Q1. What is the difference between == and === in JavaScript?
Why is === preferred in modern codebases?
🔹 Explanation:
== is the loose equality operator, which performs type coercion before comparison. === is the
strict equality operator, which checks both value and type without coercion.
🔹 Real-World Example:
false == 0 // true
[] == false // true
Always use === for predictable behavior. Convert types explicitly if needed.
A closure is when a function remembers variables from its lexical scope even after the outer
function has finished executing.
🔹 Real-World Example:
Rate Limiting:
function createRateLimiter() {
let lastClicked = 0;
const limit = 3000;
return function () {
const now = Date.now();
if (now - lastClicked > limit) {
console.log('Action allowed ✅');
lastClicked = now;
} else {
console.log('Action blocked ❌');
}
};
}
Closures are crucial for private state, debouncing, and custom React hooks.
The Event Loop is what allows JavaScript to be non-blocking and handle asynchronous
operations using a single thread. It manages the call stack, microtask queue, and callback/task
queue.
🔹 Real-World Example:
console.log('Start');
console.log('End');
Output:
Start
End
Promise
Timeout
● The promise’s .then() goes into the microtask queue and is processed before
setTimeout, which goes into the callback queue.
"Understanding the event loop helps prevent UI blocking and design responsive,
async-heavy frontend apps. I’ve debugged performance issues by analyzing task
prioritization between microtasks and macrotasks."
🔹 Real-World Example:
Use let:
this refers to the execution context. Its behavior depends on how a function is called.
Arrow functions don’t have their own this. They inherit this from the enclosing context.
🔹 Real-World Example:
const user = {
name: 'Raja',
greet: function () {
setTimeout(() => {
console.log(`Hello, ${this.name}`); // ✅ Raja
}, 100);
},
};
user.greet();
A higher-order function is one that takes another function as argument or returns a function.
Hoisting moves declarations to the top of their scope, but not initializations.
console.log(a); // undefined
var a = 10;
console.log(b); // ReferenceError
let b = 10;
🔹 Real-World Example:
function greet(lang) {
console.log(`${lang}: Hello, ${this.name}`);
}
🔹 Deep Copy:
const deep = JSON.parse(JSON.stringify(original));
const animal = {
speak() { console.log('Animal speaks'); }
};
(To be continued with Q11 to Q30 in same depth: async/await, memory leaks, module systems,
performance optimizations, currying, tail recursion, pub/sub, maps vs objects, etc.)
Both async/await and Promises handle asynchronous code. Async/await is syntactic sugar
over Promises, making the code more readable and linear.
🔹 Real-World Example:
// Promise style
fetch('/api/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.error(err));
// async/await style
async function loadData() {
try {
const response = await fetch('/api/data');
const data = await response.json();
console.log(data);
} catch (err) {
console.error(err);
}
}
Common causes:
🔹 Real-World Example:
function setup() {
const element = document.getElementById('myBtn');
element.addEventListener('click', () => {
console.log('Clicked');
});
}
If this listener is not removed when the DOM node is destroyed, it stays in memory.
🔹 Example:
// ESM
import { func } from './util.js';
// CJS
const func = require('./util');
🔹 Real-World Example:
const nums = [1, 2, 3];
nums.map(n => n * 2); // [2, 4, 6]
nums.filter(n => n > 1); // [2, 3]
nums.reduce((acc, val) => acc + val, 0); // 6
Currying transforms a function that takes multiple arguments into a series of functions that each
take one argument.
function multiply(a) {
return function (b) {
return a * b;
};
}
const double = multiply(2);
console.log(double(5)); // 10
Generators (function*) are functions that can pause using yield, then resume later.
🔹 Example:
function* idGen() {
let id = 0;
while (true) yield ++id;
}
const gen = idGen();
gen.next().value; // 1
gen.next().value; // 2
Debouncing limits how often a function is executed. Useful in search, resize, and scroll events.
🔹 Implementation:
function debounce(fn, delay) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
}
🔹 Example:
if (!Array.prototype.includes) {
Array.prototype.includes = function (val) {
return this.indexOf(val) !== -1;
};
}
● WeakMap: key must be an object. Keys are weakly referenced — prevents memory
leaks.
Ordered No Yes
Used in PWAs (Progressive Web Apps) to cache assets, handle fetch, push notifications.
✅ This completes the 30 JavaScript interview Q&A tailored for 6+ years experienced
developers in Indian tech interviews.