Important JavaScript Interview Questions and Answers
Q: What are the data types in JavaScript?
A: Primitive: String, Number, Boolean, Null, Undefined, BigInt, Symbol
Non-primitive: Object, Array, Function
Q: What is hoisting in JavaScript?
A: Hoisting is JavaScript's default behavior of moving declarations to the top. Variables declared
with 'var' are hoisted (initialized as undefined), while 'let' and 'const' are hoisted but not initialized.
Q: What is a closure?
A: A closure is when an inner function has access to the outer function's variables even after the
outer function has returned.
Q: Difference between == and ===?
A: == checks value only (performs type coercion)
=== checks value and type
Q: What is the 'this' keyword?
A: 'this' refers to the context in which a function is called. In objects, it refers to that object. In a
global function, it refers to window (in browser).
Q: What is the event loop?
A: The event loop allows JavaScript to be non-blocking and handle async operations. It moves tasks
from the call stack and callback queue/microtask queue into execution order.
Q: What is the difference between let, const, and var?
A: var - function-scoped, hoisted, can be redeclared
let - block-scoped, hoisted (but not initialized), can be updated
const - block-scoped, cannot be updated or redeclared
Q: Explain arrow functions.
A: Arrow functions are shorter function syntax and do not have their own 'this'.
Example: const greet = () => [Link]('Hi');
Q: What is a Promise?
A: A Promise represents a value that may be available now, or in the future. It has 3 states: pending,
fulfilled, rejected.
Q: What is event delegation?
A: Instead of attaching an event listener to every element, you attach it to a common parent and use
[Link] to handle specific elements.
Q: Explain debouncing.
A: Debouncing ensures a function runs only after a certain time has passed since the last time it was
invoked. Useful in input fields or resizing.
Q: Difference between null and undefined?
A: undefined means a variable has been declared but not assigned
null is an assigned value meaning 'no value'
Q: What is shallow copy vs deep copy?
A: Shallow copy copies only top-level properties
Deep copy copies all nested levels using [Link]([Link](obj))