What's going on here with these booleans?

let x = 0;
let y = 0;
console.log(x++ && y++);

Why is y still 1? I don’ t get why the first console.log instruction results in 0!

This is very confusing and if someone could please explain this I’d be very appreciative… Thanks

Hey man!
That’s because you’re using the post increment operator variableName++.
This works in the following manner, whenever you use it, it uses its current value and it then updates.

let y = 0;
console.log(y++); // First prints y current value, 0,  and then increments y + 1
console.log(y); // Prints y = 1

To further understand this, you may wanna try the ‘pre-increment’ operator.


This one, before the variable’s value is used it increments it.

Hope I mad emyself somewhat clear.

1 Like

Many thanks! Yeah, that did the trick, the evaluation x++ happens afterwards and console.log(x++ && y++); yields false and y++ is not evaluated.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.