Increment & Decrement in JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Increment & Decrement in JavaScript

Confusing, can someone give an example of how increments and decrements are used?

7th May 2017, 12:49 AM
//ismafahreen
3 Answers
+ 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++)
7th May 2017, 12:54 AM
Szabó Gábor
Szabó Gábor - avatar
+ 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.
7th May 2017, 12:54 AM
Welliton Malta
Welliton Malta - avatar