1. What is a closure in JavaScript?
A *closure* is a function that has access to its outer function's variables, even after the outer function has returned.
function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
}
}
const counter = outer();
counter(); // 1
counter(); // 22. Explain event delegation.
Event delegation is a technique where you attach a single event listener to a parent element to handle events on its child elements using
event.target.3. What is the difference between == and ===?
-
== checks for value equality (type coercion allowed). -
=== checks for both value and type (strict equality). '5' == 5 // true
'5' === 5 // false
4. What is the "this" keyword?
this refers to the object that is executing the current function. In arrow functions, this is lexically bound (based on where it's defined).5. What are Promises?
Promises handle asynchronous operations. They have 3 states: *pending, resolved, rejected*.
const p = new Promise((resolve, reject) => {
resolve("Success");
});
p.then(console.log);6. Explain async/await.
Syntactic sugar over Promises for better readability in async code.
async function fetchData() {
const res = await fetch(url);
const data = await res.json();
}7. What is hoisting?
Variables and function declarations are moved to the top of their scope before code execution.
console.log(a); // undefined
var a = 5;
8. What are arrow functions and how do they differ?
Arrow functions are shorter and do not have their own
this, arguments, or super.const add = (a, b) => a + b;
9. What is the event loop?
The event loop handles asynchronous callbacks and ensures non-blocking behavior in JS by placing them in the task queue after the call stack is clear.
10. What are IIFEs (Immediately Invoked Function Expressions)?
Functions that run as soon as they are defined.
(function() {
console.log("Runs immediately");
})();💬 Double Tap ❤️ For More