[Solved] Why does value of a variable change inside if statement


if is not only a conditional statement but also executes code written in it i.e the condition is evaluated

eg 1:

if(a == 5) {} //checking value of a

but

if(a = 5) //assigning 5 to a but also checking value of a as Undefined, null

eg 2:

Similarly in a for…loop

for(var i=0; i<10; i++)

initiation, condition check and updation of value altogether gets performed

In your case
–a will also act as a code statement and after that it will act as a conditional statement which will get executed and will return a value of 0 means you variable will get changed and ! will negate it and will make it true(though it does not matter what you are asking).

if(! (--a)) { // when i say --a i did't mean to say "change the value of a" i just meant to say "without manupulating the actual value of a , just check if --a is falsy"
  console.log('ok');
}

console.log(a);

solved Why does value of a variable change inside if statement