Why does == cause an infinite loop but the = doesn't


This causes an infinite loop

while(slaying) {
slaying == false;

This doesn't cause an infinite loop

while(slaying) {
slaying = false;




The = character is the so-called assignment-operator
With the assignment-operator you assign an associated Value to a variable

The associated VALUE can be anything from:

•Object  ◦Function/'Method'

Above you have an overview of all data-types.

In Javascript you have 2 comparison-operators

  • strict comparison operator ===
  • type converting comparison operator ==



Slaying == false compares slaying to false resulting in a not used value of probably true and leaving slaying as it is -> true, so while(true) is the definition of an infinite loop.
Slaying = false, assigns a new value of false to slaying so when you check the condition again it is false and so you stop.