Increment operator in javascript

Javascript provides increment operation. To perform increment operation, we use increment operator. Increment operators are beneficial in lots of cases. Such as, suppose a user is performing certain task repetitively. With the help of increment operator, we can track the number of times the user has performed the task.

We use increment operator to increment the value of a variable by 1. 

There are two types of increment operation exist in javascript.

Pre-increment operator (++a)

Pre-increment operator, increments self first then assigns the value.

Example:

var a=1;

var b=++a;

alert(a);

alert(b);

The above code would print 2, 2

Because, a incremented self first then assigned to the variable b.

Post-increment operator(a++)

Post-increment operator, assigns self first then increments self

Example:

var a=1;

var b=a++;

alert(a);

alert(b);

The above code would print 2, 1

Because, the value of a is assigned first to the variable b then the value of a is incremented by 1.

So we have to careful while using pre-increment operator and post-increment operator.