javascript - Difference between test expressions -
i'm implementing obligatory slider in project. , following code worked fine:
else if( current - 1 === sliderlength ){ current = 1; loc = 0 }
but didn't work expected:
else if( current === sliderlength - 1 ){ current = 1; loc = 0 }
so, difference between current === sliderlength - 1
, current - 1 === sliderlength
?
i rename variable x , y brevity, , include pertinent part:
if (x - 1 === y)
vs if (x === y - 1)
if have hard time seeing difference, try them both out random values x , y. let's try x = 3
, y = 2
so, first 1 becomes:
if (3 - 1 === 2)
, or if ((3 - 1) === 2)
, or if (2 === 2)
(i.e. true
)
second 1 becomes:
if (3 === 2 - 1)
, or if (3 === (2 - 1))
, or if (3 === 1)
(i.e. false
)
the important thing note comparison operation (===
) happens after subtraction operation. that's not important in case, note.