JavaScript is the backbone of modern web development. Whether you're preparing for an interview or refreshing your knowledge, here are 10 frequently asked JavaScript questions with concise, accurate answers.
1. What is the difference between var
, let
, and const
?
-
var
has function scope and allows redeclaration. -
let
andconst
have block scope. -
let
is mutable;const
is immutable (cannot be reassigned).
2. What is a closure in JavaScript?
A closure is a function that remembers the variables from its outer lexical scope, even after the outer function has finished executing.
function outer() {
let count = 0;
return function inner() {
return ++count;
};
}
3. What is the difference between ==
and ===
?
-
==
checks equality with type coercion. -
===
checks equality without type coercion (strict equality).
"5" == 5 // true
"5" === 5 // false
4. What is event delegation?
Event delegation is a technique where a single event listener is added to a parent element to handle events from its children, using event.target
.
Useful for performance and dynamic content.
5. What are promises in JavaScript?
A Promise is an object representing the eventual completion or failure of an asynchronous operation.
let promise = new Promise((resolve, reject) => {
// async task
});
6. What is hoisting in JavaScript?
Hoisting means variable and function declarations are moved to the top of their scope before code execution.
-
var
is hoisted (but not initialized). -
let
andconst
are hoisted but stay in temporal dead zone.
7. What is the difference between null
and undefined
?
-
undefined
: A variable declared but not assigned a value. -
null
: An intentional absence of value.
let a; // undefined
let b = null // null
8. Explain this
keyword in JavaScript.
this
refers to the object that is executing the current function:
- In a method: the object itself
- Alone:
window
(in non-strict mode) - In arrow functions: inherits from the parent scope
9. What is the event loop in JavaScript?
The event loop handles async callbacks by managing the call stack and task queue, allowing non-blocking behavior.
10. What is the difference between map()
and forEach()
?
-
map()
returns a new array. -
forEach()
executes a function for each item but returns undefined.
[1,2,3].map(x => x*2); // [2,4,6]
[1,2,3].forEach(x => x*2); // undefined
Conclusion:
Mastering these core JavaScript concepts can significantly boost your confidence in technical interviews. Practice coding each of these examples to deepen your understanding.
Top comments (0)