🚨 Stop Memorizing JavaScript. Start Predicting Its Output. 🚨 In JavaScript/Frontend interviews, output-based questions separate surface-level knowledge from real understanding. They don’t ask what you know , they test how your brain executes code. If you truly understand JS, you should be comfortable predicting outputs around: ⚡ Hoisting & Temporal Dead Zone ⚡ Closures & Scope chains ⚡ var vs let vs const ⚡ Promises, async/await, and the event loop ⚡ Type coercion & tricky comparisons ⚡ this keyword & execution context Interviewers love these questions because they reveal: 👉 Logical thinking 👉 Debugging mindset 👉 Real-world JS experience 💡 Pro tip: If you struggle with outputs, don’t avoid them — practice them daily. That’s where the biggest growth happens. 📩 Want a curated list of high-quality output-based JavaScript interview questions? Comment “JS” or DM me. I’ll share it with you. 🔁 Like • Comment • Share to help other developers prepare smarter, not harder. #JavaScript #Frontend #WebDevelopment #InterviewPreparation #CodingInterviews #100DaysOfCode #LearnJavaScript #Developers
In your code in the image posted is somehow little confused. there may be syntax error. let x = 10; setTimeout(() => { x = 20 }, 1000); console.log(x); }, - this last curly brace is unnecessary here. So if the correct code is: let x = 10; setTimeout(() => { x = 20 }, 1000); console.log(x); Then the answer is A (10)
It's got the syntax error
Actually it will throw an syntax error, as there is the mismatch in the arrow and there is an extra }, is present. If its resolved then the value of x is 10. Execution Order is : 1. x = 10 2. setTimeout scheduled (callback queued for 1000ms later) 3. console.log(x) → prints 10 4. Callback runs, x = 20 (but nothing logs this) To get 20, let x = 10; setTimeout(() => { x = 20; console.log(x); // This will print 20 after 1 second }, 1000); console.log(x); // Still prints 10 immediately
What's the purpose of the last line }, ?
I see the confusion 👍 The -> is just text formatting, not JavaScript syntax. And }, is valid here because it correctly closes the arrow function and separates arguments in setTimeout. The snippet itself is syntactically valid. The output is A) 10 due to async execution — console.log(x) runs before the callback updates x.
I hate this approach not because I can't predict that answer is 10. But because your brain should give real results based on some code. You don't need to be compilator. You are living in real world you should evaluate problem solving skill, not replacing him instead of JavaScript compilator. Yes most of good devs can solve those puzzles, but good problem solvers may ask. WTF? why I need to work as compilator if it is not even primary part of my job ? and they will be right as compilator is working automatically based on tools, doing something instead of tools is bad approach. saying something like you are using tool and don't understand how it is making what it happens ? Is totally wrong, you may study line by line compile read code and say exact predictable output. But may just work with results and never get deep dive into processes under meet the hood. Good drivers are not always those who knows every corner of the car, sometimes they just work based on instincts and wins.
There are syntax errors while passing the Call back function to the setTimeout though. But if I assume all the syntax are right then the ans is 10. Why cz when setTimeout encounter with some time out then it go to the Browser for async work and there as soon as the time completed it goes to Call back Queue and as soon as Call Stack got empty the Event loop passes the Call back function to the stack and it executed so basically at that very time as JavaScript is sync single threaded language it continues the execution of code and prints the value 10. But after all that x value got updated but never console logged.
Err.. Well, the output would be something like: "...Uncaught SyntaxError: Unexpected token ')'... etc." - but that's going with the literal impl. in which the arrow function has a syntax error (arrow has a dash rather than equals sign) ... `setTimeout(() ->` ; safe to say `setTimeout(() =>` was intended there, no ? In that case -- I'd say 10.
The question here demands your good understanding of Event Loops, and if you want to practise a level up game, do check out this post below. 📌 STUCK? No problem. Just go to the comment, and see a thorough Medium post talking more about it, and helping you understand the async operations in JS in-depth. Link: https://www.garudax.id/posts/alokk830_javascript-promise-based-interview-output-activity-7404433312145993728-FcGI?utm_source=share&utm_medium=member_desktop&rcm=ACoAABnFhtABMChPlYPyobMpZFxImH366ez0xqM
Shoutout to everyone who analysed the code like a compiler🙌 The code will output a syntax error!