+ 3
Increment & Decrement in JavaScript
Confusing, can someone give an example of how increments and decrements are used?
3 ответов
+ 2
x++ uses x value than increase it.
++x increase x than use the increased value.
Example:
var x = 1;
// ++x increase x to 2 , y = 2+2 , then ++ increase x to 3.
var y = ++x + x++;
// 5 3 will alerted cause ++y increase y to 5 but x++ increase x to 4 after alert
alert(++y+' '+x++)
+ 2
increment does the operation/assignment before outputing.
var x = 1 ;
alert(++x); //this will output 2.
decrement does the operation/assignment after outputing.
var x = 1;
alert(x++) ; // this will output 1, just after this the value of x will be 2.