While and do while loop question


can someone explain to me while loop

code :
while(a = true){
a = false

why is this an infinite loop? isnt it supposed to execute the whole code block ?
Meaning i should get 2 printed out only once after which a becomes false ?


you lack an initial a = true statement in this snippet :slightly_smiling:
But the real problem is that you use = instead of == or ===. Which means that you assign true to a and an assignment has the value you assign e.g.

console.log(a=2); --> 2


Well, if you want to compare you should two equal signs (==), not one. (or three equal signs if you also want the data type too match).

You can also leave out the whole comparison, and just do: while (a). This will check if a is true/false. And run if true, and stop if false


thank you for your answers