0
Please somebody explain me var x= 8; var a = (x++)% 3; document.write(a);
Javasript
4 Respostas
+ 5
Bikash Jyoti Bhagawati
The trick to understanding this is to understand prefix & postfix incrementation.
Your code returns 2, because 8%3=2.
The statement is assessed before x is incremented.
This code will return 0
var x= 8;
var a = (++x)% 3;
document.write(a);
x is incremented before the statement is assessed, so
9%3=0
+ 3
X++ uses x value then increments it
++X increments x value then use it
So, 8%3 is 2
you should understood postfix and prefix
+ 3
well. x++ doesn't actually increment until the next line, and modulus operator returns the remainder. so that's why 8 % 3 = 2