Please somebody explain me var x= 8; var a = (x++)% 3; document.write(a); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please somebody explain me var x= 8; var a = (x++)% 3; document.write(a);

Javasript

28th Aug 2021, 9:43 AM
Bikash Jyoti Bhagawati
Bikash Jyoti Bhagawati - avatar
4 Answers
+ 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
28th Aug 2021, 9:53 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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
28th Aug 2021, 9:48 AM
Vtec Fan
Vtec Fan - avatar
+ 3
well. x++ doesn't actually increment until the next line, and modulus operator returns the remainder. so that's why 8 % 3 = 2
28th Aug 2021, 9:49 AM
Rellot's screwdriver
Rellot's screwdriver - avatar