these operators in that position actually give you the "old" value back. So the expression num++ gives you a 0 (which is a falsey value).
num++
0
try this in the browser console and you'll see.
var num = 0; var result = num++; if(result) { console.log('Number'); } else { console.log({ result, num }); }
I tried what you did and this:
var num = 0; num++; var result = num++; if(result) { console.log('Number'); } else { console.log({ result, num }); }
...and That answered my question. So what happens is if the number incremented by any value, the result will be true.
Every number except 0 and NaN is considered a "truthy value". And if something is "truthy" then it pass the evaluation of the if statement.
NaN
if
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
these operators in that position actually give you the "old" value back. So the expression
num++
gives you a0
(which is a falsey value).try this in the browser console and you'll see.
I tried what you did and this:
...and That answered my question. So what happens is if the number incremented by any value, the result will be true.
Every number except
0
andNaN
is considered a "truthy value". And if something is "truthy" then it pass the evaluation of theif
statement.