Control flow - 3/11 - Why must there be so many = equal marks in the if statement?


#1

Why must there be so many = equal marks in the if statement?

 let myName = true

if (myName === true) { . . .



#2

There are three forms…

=      denotes assignment

a = 6;
b = 7;

==     denotes similarity

a == '6'     // <-  true

===    denotes equality or identity

a === b      // <-  false
a === '6'    // <-  false
b === 7      // <-  true

We never use a single equals sign in a comparison, as the interpreter will always see it as an assignment of the value or expression from the right side to the variable on the left side.

In practice, we rarely use the double equals sign since it involves coercion (data type conversion) on the fly. JavaScript data is loosely typed, and JS being a determined interpreter always tries its best to make something out of everything. Above, we coerced the string entity, '6' into a numeric value that could then be compared to a.

For most situations, we will want to design our code so that datatypes are the same, and compare with the strict operator, ===.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.