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


#1

This causes an infinite loop

while(slaying) {
slaying == false;
}

This doesn't cause an infinite loop

while(slaying) {
slaying = false;
}

Why?


#2

@textrockstar34699,

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:

•Number
•String
•Boolean
•Symbol
•Object  ◦Function/'Method'
         ◦Array
         ◦Date
         ◦RegExp
         ◦Math
•Null
•Undefined

Above you have an overview of all data-types.

In Javascript you have 2 comparison-operators

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators


#3

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.