JavaScript Synchronous Execution Leads to Infinite Loop

One interviewer asked me this question: "What is the output of this code?" Most of us might think that once the setTimeout or Promise executes, right will become true, the while loop will close, and "Bye" will be printed. But that’s not the case. Because JavaScript is synchronous and single-threaded, it waits for no one learnt from some great people who also said "Time tide and Javascript waits for none 'Akshay Saini 🚀 The Block: Once the engine sees the while loop, it keeps executing it over and over. The Starvation: This creates an infinite loop that occupies the Call Stack entirely. The Deadlock: Even though the setTimeout and Promise are ready to change the variable, the Event Loop is never called because the stack never clears. The result? A frozen browser tab and a console full of "I am right," but never a "Bye." It’s a great reminder: Never block the main thread! #JavaScript #WebDevelopment #CodingInterviews #EventLoop #ProgrammingTips #FrontendDeveloper

  • graphical user interface, text, application

Hello I am right I am right I am right... it's never stop but if you want to stop then you can await the Promise then execution will stope until the Promise resolve and then right variable will become true and after that it will never stuck in while loop because right is true so output is ... Hello Bye

HelloI am rightI am right..........never stops because javascript is single threaded loop takes the Call Stack , Promise and setTimeOut needs empty call stack to run

Hello I am right will run into infinity loop

Hello I am ready ( I am ready string will runs infinity loop and bye will not execute)

Like
Reply

Hello and i am right..to infinite

Stupid question!!! Or stupid interviewer!!!

See more comments

To view or add a comment, sign in

Explore content categories