1 === variable1
is same as the expression variable1 === 1
written in Yoda notation**: constant listed on left hand side, variable on the right hand side.
The main reason why some programmers choose to use it is to avoid the common mistake of writing if (a = 1)
where the programmer actually meant if (a == 1)
or if (a === 1)
. The following line of code will work but not as expected (a
is assigned a value and if
block will always get executed):
if (a = 1) {}
The same expression written the other way round will generate a syntax (or compile) error:
if (1 = a) {}
The programmer can immediately spot the error and fix it.
I do not like or use the Yoda notation. I try to keep my eyes open while coding.
** I am unable to find out the origin of this term.