JavaScript Output Questions for 3 Years Experience: Frontend Interview Prep
At 3 years of experience, JavaScript interviews test whether you can predict output, not recite definitions. This post covers the highest-volume output questions: closures with setTimeout, type coercion, and this binding.
- I have watched students nail theory questions in frontend interviews, then freeze when the interviewer wrote a five-line function on the whiteboard and asked, "What does this print?"
- They could define a closure perfectly. They could not tell you what
varandletinside asetTimeoutloop actually output.
- At 3 years, interviewers assume you have built real apps, so they probe the quirks that break real apps — event loop ordering, reference vs value, coercion.
- That gap is the entire interview at this level.
What does this print? for (var i=0; i<3; i++) { setTimeout(()=>console.log(i), 100) } — Most 3-year developers get this wrong. Here's why.
At 3 years of experience, JavaScript interviews test whether you can predict output, not recite definitions. The questions that actually appear in frontend rounds focus on closures with setTimeout, type coercion, and this binding. Each one proves you understand how the engine executes code, not just what the syntax looks like.
These topics dominate at the 3-year mark because they are the quirks that break real apps. This post walks through the output questions from javascriptquiz.com and a real Tech Mahindra interview gist, with the reason each answer matters. You can verify the main claims on this sample of tricky questions.
Why does the loop print 3 3 3 with closures and setTimeout?
A var loop variable is shared across all setTimeout callbacks, so by the time the timer fires, the loop has finished and every callback sees the final value. This is the single most asked output question at this level, per javascriptquiz.com's list of 100 tricky questions. Here is the code you will see:
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
// Output: 3 3 3
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
// Output: 0 1 2
var is function-scoped, so all three callbacks close over the same i. let is block-scoped, so each iteration creates a fresh binding for its callback. The Tech Mahindra interview gist confirms this exact question appears in real L1 rounds. What the interviewer hears when you answer correctly: you understand variable scope and how closures capture values, not just that "closures exist."
| Question | Output | What your answer proves |
|---|---|---|
var in a loop with setTimeout | 3 3 3 | You know var shares one binding across all closures |
let in a loop with setTimeout | 0 1 2 | You know let creates a new binding per iteration |
typeof null | 'object' | You know legacy bugs in the language, not just the simple, common case |
Type coercion and number quirks
The less-than operator compares strings character by character, and the character '1' has a smaller code than the character '9', so the string '10' is "less than" '9'. No numeric conversion happens when both sides are strings, per javascriptquiz.com. This trips up people who assume JavaScript always converts to numbers. Run these in your console:
'10' < '9';
// true — compares '1' with '9' as characters
0.1 + 0.2 === 0.3;
// false — binary floating point gives 0.30000000000000004
[] + {};
// "[object Object]" — empty array converts to "", then to the object's string
1 + '2' + 3;
// "123" — first addition makes a string, the rest follow
-7 % 3;
// -1 — the remainder takes the sign of the dividend
Each of these hides a conversion rule. 0.1 + 0.2 fails because numbers are stored in binary, and 0.1 has no exact binary representation. [] + {} shows how the + operator forces both sides to primitives. Answering these correctly signals you have debugged real code, not just read a tutorial. The Tech Mahindra gist lists 1+'2'+3 and []+{} among its common 25 output questions, so expect them in service-based company rounds too.
Why do arrow functions ignore call, apply, and bind?
Arrow functions have no own this, so call, apply, and bind cannot change what this refers to inside an arrow function. They are ignored entirely, as javascriptquiz.com explains in its output questions. A regular function takes the this you pass; an arrow function uses the this from where it was defined.
| Code | Output | Why |
|---|---|---|
obj.method() | obj | Method call sets this to the object before the dot |
const f = obj.method; f() | undefined (in strict mode) or the global object (in non-strict mode) | Detached call loses the object context |
arrow.call(obj) | the this from where the arrow was defined, not the object passed to call | Arrow ignores call — no own this exists |
This matters in React event handlers. If you pass an arrow function as a prop, this comes from the component's lexical scope. If you pass a regular function, this depends on how it is called. The interviewer wants to see you connect a language quirk to a framework behavior. The Tech Mahindra gist pairs these to test whether you know the difference between strict equality and Object.is.
Why do microtasks run before macrotasks in the event loop?
Promise callbacks (microtasks) always run before setTimeout callbacks (macrotasks), no matter how the code is ordered. This is the foundational rule of the JavaScript event loop explained on javascript.info. Here is the classic example:
console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => console.log(3));
console.log(4);
// Output: 1 4 3 2
The synchronous code runs first (1, 4). The Promise callback goes to the microtask queue; the setTimeout goes to the macrotask queue. The engine drains all microtasks before it touches the next macrotask, so 3 prints before 2. javascript.info's event loop page states that microtasks run completely before the next macrotask, and rendering never happens mid-task. The Tech Mahindra gist confirms a real L1 interview asked exactly this microtask/macrotask ordering question, so treat it as a must-know.
Talk through it out loud in the interview: "Synchronous code prints 1 and 4. The Promise callback queues as a microtask, the setTimeout as a macrotask. Microtasks finish first, so 3 comes before 2." That spoken explanation is what convinces the interviewer you understand it.
Why is a==b false and why does b.name change a.name?
Objects and arrays compare by reference, so two separately created objects are never equal, and changing a property through one reference changes it for all references. This is the reference semantics the Tech Mahindra interview gist tests directly.
let a = [1, 2];
let b = [1, 2];
a == b;
// false — different references, even though contents match
let obj1 = { name: 'A' };
let obj2 = obj1;
obj2.name = 'B';
console.log(obj1.name);
// "B" — both names point to the same object
Two arrays with identical contents are still two different objects in memory. Comparing them checks if they are the same object in memory, not if their contents are the same. When you assign obj2 = obj1, you copy the reference, so mutating obj2.name changes what obj1.name reads.
This matters in React: React relies on new references to detect changes, so mutating an object in place can prevent re-renders. Correct answers here show you understand why immutability matters in modern frontend frameworks, and that is what the interviewer is really probing.
What are the common output traps: NaN, typeof null, and the parseInt map?
Three questions — typeof NaN being 'number', typeof null being 'object', and [1,2,3].map(parseInt) returning [1,NaN,NaN] — show up constantly because each hides a language quirk from the Tech Mahindra gist's list of common output questions.
typeof NaNAnswer:
'number'. NaN stands for "Not a Number" but the spec defines it as a number type.typeof nullAnswer:
'object'. A legacy bug from early JavaScript that no one fixes because it would break existing code.NaN === NaNAnswer:
false. NaN is not equal to anything, including itself. UseNumber.isNaN()to check for it.[1,2,3].map(parseInt)Answer:
[1, NaN, NaN].mappasses the index as the second argument, andparseIntuses it as the radix.null == undefinedAnswer:
truewith==,falsewith===. Loose equality treats them as equal; strict equality does not.Array.push()return valueAnswer: the new length, not the array. Many people assume it returns the array itself.
- Question 4 deserves extra attention.
parseInttakes a string and a radix.mapcallsparseInt('1', 0),parseInt('2', 1),parseInt('3', 2).- Radix 0 means the engine guesses the base from the string, radix 1 is not a valid base for numbers, so it fails, and binary (base 2) cannot represent the digit 3, so those calls return NaN.
- Quick reference:
parseInt('1', 0)→ 1,parseInt('2', 1)→ NaN,parseInt('3', 2)→ NaN. - The interviewer watches whether you know function signatures, not just array methods.
How can you prepare for output-based questions in a week?
Practice one output question daily, write your reasoning before looking at the answer, and for each question, explain why the engine behaves that way out loud — this method builds the mental model javascriptquiz.com's 100 output-based questions are designed to train.
Predict first
Read the code and write the exact output you expect, including spaces between values.
Run it
Paste the code into your browser console and compare the real output with your prediction. In Chrome, press F12 or right-click and choose Inspect, then click the Console tab.
Find the rule
For every mismatch, identify the language rule at play: scope, coercion, reference, or queue ordering.
Explain out loud
Say the reasoning as if the interviewer is listening. This converts passive knowledge into interview answers.
Start with the questions in this post. Then move to the advanced JavaScript questions for 5 years experience when the basics feel automatic. You can also revisit your fundamentals with JavaScript projects for beginners if the underlying concepts feel shaky. Output questions only expose what you already know. They are not a shortcut to learning the language.
Pick five output questions from the sources below, run each in your browser console, and explain each result out loud before your next interview.
## FAQ Do I need to memorize all these? No. Interviewers look for reasoning, not memory. If you can explain why the output happens, you don't need to memorize each example. How do I practice without a partner? Use your browser console and speak your reasoning out loud to yourself.
Recording your explanation and playing it back helps even more. Are these the same for service-based vs product-based companies? Service-based interviews often reuse known questions like the ones in the Tech Mahindra gist. Product-based companies tend to modify them, so understanding the rule matters more than the exact question. What about async/await output questions? Async/await is syntactic sugar over Promises, so the same microtask rule applies: await yields to the microtask queue. Practice rewriting async/await as .then chains to see the ordering.
For deeper reference, see MDN's guide to closures and MDN's event loop page.