- Post Increment : variableName ++
- Pre Increment : ++ variableName
Example of post increment
If we suffix a variable name by increment operator it’s called post-increment. The post-increment operator is Increment is done only after printing the value. For example, you have the value=10, if you use post-increment then the value ’10’ is printed then value increases to 11
var a=10; document.write("a= ",a,"<br/>"); a++; document.write("a= ",a); /*Output: a= 10 a= 11 */
- In between the script tag, we have a variable ‘a’ and we assigned to the value ’10’.
- Then display the value of ‘a’ by using document.write method.
- Use the post-increment operator to increment the value by 1. So, on this line, the ‘a’ value becomes ’11’.
- And display the ‘a’ value after the increment. See the above output, the value of ‘a’ is displayed 11. So, this the usage of the post-increment operator.
Example of pre-increment
If we prefix a variable name by increment operator it’s called a pre-increment. The pre-increment operator is the value is printed after incrementing. For example, you have the value=10, if you use pre-increment then the value increases to 11 then the value ’11’ is printing.
var a=10; document.write("a= ",++a,"<br/>"); /*Output: a= 11 */
- In between the script tag, we have a variable ‘a’ and we assigned it to the value ’10’.
- The value of ‘a’ is printed after the value of ‘a’ is incremented.