Increment & Decrement in JavaScript | Sololearn: Learn to code for FREE!

+3

Increment & Decrement in JavaScript

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

5/7/2017 12:49:07 AM

//ismafahreen

3 Answers

New Answer

+7

https://www.sololearn.com/discuss/331665/?ref=app https://www.sololearn.com/discuss/161132/?ref=app https://www.sololearn.com/discuss/245871/?ref=app

+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.