DEV Community

[Comment from a deleted post]
Collapse
 
gargakshit profile image
Akshit Garg

putting ++ before a variable name means incrementing it before use, and putting it after the variable name means incrementing after use. same goes for --

So

let a = 0; // this is falsey because 0

if (a++) { // here a will be checked by if and then become 1
  console.log("I won't run, a =", a);
} else {
  console.log("I will run, a =", a); // This will print "I will run, a = 1"
}
Enter fullscreen mode Exit fullscreen mode

and

let a = 0; // this is falsey because 0

if (++a) { // here, a will become 1 before being checked with if
  console.log("I will run, a =", a); // This will print "I will run, a = 1"
} else {
  console.log("I won't run run, a =", a);
}
Enter fullscreen mode Exit fullscreen mode

Feel free to try these snippets in your browser console.