1

I’ve been playing around with some code, and I’ve discovered that the following does not act as expected:

var truth = true;
var x = 0;

setTimeout( ()=> truth = false, 10);

while(truth === true){
  console.log(x+=1)
}

My assumption was that the while loop would be ended after the callback in setTimeout changed the “truth” value, but the while loop seems to run infinitely...

Can someone explain why? I’m definitely missing an aspect of Javascript which can explain this behavior!

Thank you!

0 Answers0