Member-only story
5 JavaScript Output-Based Problems to Practice Before Your Next Interview
JavaScript output-based questions are a staple in coding interviews. They test your understanding of core concepts like hoisting, closures, async operations, and object immutability.
Here are five tricky problems you should master:
— -
1. What will the following code output, and why?
console.log(a);
var a = 10;
console.log(b);
let b = 20;
Answer:
undefined
ReferenceError: Cannot access ‘b’ before initialization
Explanation:
1. var a: Variables declared with var are hoisted to the top of their scope and initialized with undefined. Hence, console.log(a) outputs undefined.
2. let b: Variables declared with let are also hoisted, but they remain in the “temporal dead zone” until their declaration is encountered. Accessing b before its declaration results in a ReferenceError.
— -
2. What will the following code output, and how can you ensure a deep copy?
let obj1 = {
name: 'John',
address: {
city: 'New York',
zip: '10001'
}
};
let obj2 = {…