JavaScript
JavaScript
May 24, 2025 at 05:47 AM
*10 professional-level JavaScript interview questions with answers:* *1. What is the difference between var, let, and const?* var is function-scoped and can be hoisted. let and const are block-scoped and not hoisted like var. const cannot be reassigned after declaration. var a = 10; let b = 20; const c = 30; *2. What is a closure in JavaScript?* A closure is a function that retains access to its lexical scope even after the outer function has finished executing. function outer() { let count = 0; return function inner() { count++; return count; }; } const counter = outer(); counter(); // 1 counter(); // 2 *3. How does the JavaScript event loop work?* The event loop handles async tasks by pushing them into the call stack only when it's empty. Microtasks (like Promises) are prioritized over macrotasks (like setTimeout). *4. Explain this keyword in JavaScript.* this refers to the context in which a function is called. In a method: this refers to the object. In a regular function: this refers to window (in non-strict mode). In arrow functions: this is lexically inherited. *5. What is the difference between == and ===?* == checks for value equality (type coercion). === checks for strict equality (no type coercion). '5' == 5 // true '5' === 5 // false *6. What is event delegation?* Event delegation is a technique where a single event listener is added to a parent element to handle events on its child elements. document.getElementById("list").addEventListener("click", function(e) { if (e.target.tagName === "LI") { console.log("List item clicked:", e.target.textContent); } }); *7. What is the difference between null and undefined?* undefined: a variable declared but not assigned a value. null: an assignment value representing no value. let a; console.log(a); // undefined let b = null; console.log(b); // null *8. What is a Promise and how do async/await work?* A Promise represents the eventual result of an asynchronous operation. async/await make asynchronous code look synchronous. async function getData() { let result = await fetch('https://api.example.com'); let data = await result.json(); console.log(data); } *9. What is prototypal inheritance?* JavaScript objects inherit properties and methods from a prototype object. function Person(name) { this.name = name; } Person.prototype.sayHi = function() { return `Hi, I'm ${this.name}`; }; *10. What is debouncing in JavaScript?* Debouncing limits the rate at which a function is executed, waiting for a pause in activity. function debounce(fn, delay) { let timer; return function(...args) { clearTimeout(timer); timer = setTimeout(() => fn.apply(this, args), delay); }; } *React ❤️ for more*
❤️ 👍 😂 🙏 🤣 40

Comments