Facebook Pixel

JavaScript Interview: Output Prediction Questions

Output prediction questions test event loop and concept understanding. Here are examples.

JavaScript Interview: Output Prediction Questions

Output prediction questions are a favorite interview format. Here are examples.

Q1: Event Loop

console.log("1"); setTimeout(() => console.log("2"), 0); Promise.resolve().then(() => console.log("3")); console.log("4"); `` **Answer**: 1, 4, 3, 2 (sync > microtask > macrotask). ### Q2: Hoisting ```js console.log(a); var a = 5; `` **Answer**: undefined (var is hoisted with undefined). ### Q3: TDZ ```js console.log(b); let b = 5; `` **Answer**: ReferenceError (let is in TDZ). ### Q4: Closures Loop ```js for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); } `` **Answer**: 3, 3, 3 (var shares one i). ### Q5: this Keyword ```js const obj = { x: 10, foo: () => console.log(this.x) }; obj.foo(); `` **Answer**: undefined (arrow inherits lexical this). ### Q6: Promise Chaining ```js Promise.resolve(1).then(n => n + 1).then(n => console.log(n)); `` **Answer**: 2 (each .then passes its return). ### Q7: async/await ```js async function foo() { console.log("1"); await Promise.resolve(); console.log("2"); } foo(); console.log("3"); `` **Answer**: 1, 3, 2 (await pauses, rest is microtask). ### How to Prepare 1. Know the event loop order: sync > microtask > macrotask. 2. Know hoisting: var (undefined), let/const (TDZ). 3. Know closures: capture variable, not value. 4. Know `this`: 5 binding rules. 5. Practice 20+ output prediction questions. ### The Takeaway Output prediction questions test: event loop (sync > microtask > macrotask), hoisting (var vs let/const), closures (loop bug), this keyword (arrow vs regular), promise chaining (return values), and async/await (microtask scheduling). Practice 20+ questions.

1, 4, 3, 2. Sync code (1, 4) runs first. Then microtasks (3, Promise.then). Then macrotasks (2, setTimeout). This is the fundamental event loop order.

3, 3, 3. var is function-scoped; all callbacks share one i, which is 3 by the time they run. Use let to fix (logs 0, 1, 2).

1, 3, 2. Code before await runs synchronously (1). await pauses the function and schedules the rest as a microtask. Code after the foo() call runs (3). Then the rest of foo runs (2, microtask).

Know the event loop (sync > microtask > macrotask), hoisting (var=undefined, let/const=TDZ), closures (capture variable not value), this (5 binding rules), and promise chaining (return values). Practice 20+ questions.

undefined. Arrow functions inherit this from their lexical scope, not from the object. The enclosing scope's this does not have x. Use a regular function for methods: foo() { console.log(this.x); }.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.