javascript - Using the -- operator in a while loop produces unexpected results -
in formula,
var x = 5; while(x--) { console.log(x) }
the output is
4 3 2 1 0
why 0
output? on fifth iteration, x--
equal 0
falsey, loop should not run.
on same token, returns true
var x = 1; (x--) == true;
but returns false
0 == true
x-- evaluates first , decrements variable. if use --x instead, expect.
Comments
Post a Comment