Most Important JavaScript Output-Based Interview Questions and Answer
These types of questions test how well you understand how JavaScript works under the hood—things like hoisting, closures, promises, and scope.
1. Hoisting and var
Output: undefined
Why: JavaScript moves all var declarations to the top of their scope — this is called hoisting. But only the declaration is hoisted, not the value.
It's like the code becomes:
var a;
console.log(a); // undefined
a = 10;
So, when you log a, it exists but has no value yet.
2. Function Hoisting
Output: Hello
Why: Function declarations (not expressions) are hoisted completely, including their body. That means you can use the function before it appears in the code.
Behind the scenes:
function foo() {
console.log("Hello");
}
foo();
So, foo() works fine even before the function line.
3. var in Loops with setTimeout
Output:
Why:
var is function-scoped, so all three callbacks share the same variable i. After the loop, i becomes 3. Then all timeouts run and print the same value: 3.
👉 Use let instead if you want different values.
4. Closures in Loops
Output:
Why: Here, an IIFE ((function(x){...})(i)) is used. It captures the current value of i as x, so each callback has its own copy.
This is how closure helps remember x.
5. this in Regular Functions
Output: Grok
Why: When a method is called using obj.sayName(), the this inside points to obj. So this.name becomes "Grok".
6. Arrow Function this
Output: undefined
Why: Arrow functions don’t get their own this. They use the this from their outer scope — usually the global object. In that case, this.name is undefined.
7. Event Loop: Promises vs setTimeout
Output:
Why: JavaScript handles async code with the event loop:
-
console.log runs first
-
Promise.then() is a microtask — it runs before setTimeout
-
setTimeout is a macrotask
So:
→ Start
→ End
→ Promise
→ Timeout
8. Variable Shadowing
Output: 20
Why: The inner variable x in the function hides the outer x. This is called shadowing — the local variable is used instead.
9. Block Scope with let and const
Output: ReferenceError
Why: let and const are block-scoped. They're only available inside the { }. Outside the block, they don’t exist.
10. Closures (Counter Example)
Output:
Why: The inner() function still has access to count, even after outer() is done. This is a closure — when a function “remembers” variables from where it was created.
11. Prototype & Inheritance
Output: Hello, Alice
Why: Methods on the prototype are shared across all objects created using new Person(). this.name refers to the person’s name.
12. Strict Mode
Output: ReferenceError
Why: In strict mode, you must declare variables using let, const, or var. Without that, JavaScript throws an error.
13. Default Parameters
Why: If no value is passed, "Guest" is used as default. This helps avoid undefined values.
14. Array Mutation with const
Output: [1, 2, 3, 4]
Why: const means you can’t reassign the variable (arr = []), but you can change the contents of the array.
15. Destructuring Assignment
Output: 1 2
Why: Destructuring pulls values directly from the object and stores them in variables.
16. Promise Chaining
Output: 2
Why: Each .then() passes the result to the next one.
So: 1 → .then(val + 1) → 2 → print 2
17. IIFE Scope
Output: ReferenceError
Why: IIFE creates a new scope. Variables inside it are not visible outside.
18. Type Coercion
Why:
-
"5" + 3 → string + number → converts 3 to "3" → "53"
"5" - 3 → string becomes number → 5 - 3 → 2
19. Object Property Keys
Output: number one
Why: All object keys are strings. So "1" and 1 refer to the same key. The last one (1: "number one") replaces the first.
20. Async/Await
Output: Done (after 1 second)
Why: The await pauses the function until the promise finishes. After 1 second, it prints "Done".
Also Read
FAQ (Simple Answers)
What are output-based questions in JavaScript?
You’re given some code and asked what it prints (the output). It checks your logic and knowledge of JS internals.
Why do interviewers ask these?
Because it shows if you understand how JS really works — like scopes, closures, and async code.
How can I practice them?
Try to predict output, then run and debug it. Use tools like in your console.
Should I memorize answers?
No. Try to understand the why behind each output.