You are currently viewing Javascript Increment by 1 | Explained

Javascript Increment by 1 | Explained

Javascript Increment by 1

In this article about javascript increment by 1. You can add 1 value by using the javascript increment operator.

Increment Operator

The increment operator is used to increase a variable value by 1. double plus (++) sign indicates the increment operator. There are two types of increment operators in JavaScript, they are :

  • Post Increment : variableName ++
  • Pre Increment : ++ variableName

The increment operator accepts only one operand is the variable name. As they accept only one operand increment and decrement operators are called unary operators. Let’s proceed and understand, how we use the increment operators in javascript.

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
*/
  1. In between the script tag, we have a variable ‘a’ and we assigned to the value ’10’.
  2. Then display the value of ‘a’ by using document.write method.
  3. Use the post-increment operator to increment the value by 1. So, on this line, the ‘a’ value becomes ’11’.
  4. 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
*/
  1. In between the script tag, we have a variable ‘a’ and we assigned it to the value ’10’.
  2. The value of ‘a’ is printed after the value of ‘a’ is incremented.

Leave a Reply