1 === variable1 matches the expression variable1 === 1 written in the Yoda ** notation **: the constant indicated on the left side, the variable on the right.
The main reason some programmers prefer to use this is to avoid a common mistake when writing if (a = 1) , where the programmer meant if (a == 1) or if (a === 1) . The following line of code will work, but not as expected ( a assigned a value, and the if block will always be executed):
if (a = 1) {}
The same expression, written in reverse order, will generate a syntax (or compilation) error:
if (1 = a) {}
The programmer can immediately notice the error and fix it.
I don't like or don't use Yoda notation. I try to keep my eyes open while coding.
** I cannot find out the origin of this term.
Salman a
source share