jeudi 11 juillet 2019

Why increment not saves in conditional operator? [duplicate]

This question already has an answer here:

I wonder why in this code counter is not saving after it is incremented? I mean now the behavior is the same as if I used if (counter+1 % 2). But ++ operator supposed to permanently increment the variable.

let counter=0;
for (let i = 0; i < 10; i++) {
    console.log(counter,'every')
    if (counter++ % 2) console.log(counter,'odd');
}

Aucun commentaire:

Enregistrer un commentaire