JavaScript Interview Q&A¶
Essential questions for frontend developer interviews.
1. What is the difference between var, let, and const?¶
- var: Function scoped, can be redeclared, hoisted.
- let: Block scoped, cannot be redeclared in same scope.
- const: Block scoped, cannot be reassigned (immutable reference).
2. What is this keyword?¶
The this keyword refers to the object it belongs to.
- In a method, this refers to the owner object.
- Alone, this refers to the global object.
- In a function, this refers to the global object.
- In an event, this refers to the element that received the event.
3. What is hoisting?¶
Hoisting is JavaScript's default behavior of moving declarations to the top. Variables defined with var are hoisted but not initialized. let and const are hoisted but not initialized (Temporal Dead Zone).
4. What is a Closure?¶
A closure is a function having access to the parent scope, even after the parent function has closed.
const add = (function () {
let counter = 0;
return function () {counter += 1; return counter}
})();
add(); // 1
add(); // 2
5. What is the Event Loop?¶
The Event Loop is what allows JavaScript (which is single-threaded) to perform non-blocking I/O operations (like setTimeout or fetch) by offloading operations to the system kernel whenever possible.
Study Hard
Understanding Closures, Promises, and the Event Loop is what separates a junior developer from a mid-level/senior developer.