We use the increment & decrement operators to increase or decrease the variable‘s value by one. JavaScript uses the ++
(increment) and — (decrement) to denote them. We can either prefix or postfix these operators. Increment & decrement operators operate on a single operand. Hence they are unary operators.
Table of Contents
Syntax of Increment & Decrement Operator
Increment Operator ++x
or x++
. This is equal to x=x+1
Decrement Operator --x
or x--
. This is equal to x=x-1
Example of increment Operator.
1 2 3 4 5 | let x=10 ++x; //increment the value by one x=x+1 console.log(x); //11 |
1 2 3 4 5 | let x=10 x--; //decrement the value by one x=x-1 console.log(x); //9 |
Example of a decrement Operator.
1 2 3 4 5 | let x=10 --x; //decrement the value by one x=x*1 console.log(x); //9 |
1 2 3 4 5 | let x=10 x--; //decrement the value by one x=x*1 console.log(x); //9 |
Prefix & Postfix
There are two ways you can use the operator. One is before the operand, which is known as the prefix. The other method is to use it after the operand, known as Postfix.
Prefix Example
1 2 3 4 5 6 7 | let a=10; ++a; console.log(a) // 11 --a; console.log(a) // 10 |
Postfix Example
1 2 3 4 5 6 7 | let a=10; a++; console.log(a) // 11 a--; console.log(a) // 10 |
Difference Between Prefix & Postfix
When we use the ++
operator as a prefix as in ++a
- The value of the variable
a
is incremented by 1 - Then it returns the value.
1 2 3 4 5 6 | a=10; b=++a; //a is incremented, a is then assigned to b console.log(b); //11 console.log(a); //11 |
1 2 3 4 5 6 | a=10; b=--a; //a is decremented, a is then assigned to b console.log(b); //9 console.log(a); //9 |
When we use the ++
operator as a Postfix as in a++
,
- The value of the variable is returned.
- Then the variable
a
is incremented by 1
1 2 3 4 5 6 | a=10; b=a++; //a is assigned to b, then a is incremented console.log(b); //10 console.log(a); //11 |
1 2 3 4 5 6 | a=10; b=a--; //a is assigned to b, then a is decremented console.log(b); //10 console.log(a); //9 |
Precedence of the Increment & Decrement Operators
The increment & Decrement operators has a higher precedence than most other operators in JavaScript. You can refer to the Operator Precedence to know more about it.
In the code below, a
is decremented first and then multiplied. Because — has higher precedence than multiplication.
1 2 3 4 5 | a=10; b=5*--a; console.log(b); //45 |
In the following example a is returned for multiplication (a*10) and then decreased. Hence you will get the result 50
1 2 3 4 5 | a=10 b=(5*a--) console.log(b); //50 |
In the example below, a is returned for multiplication (5*10) and then decremented by 1. The new value of a is then added to the result (50+9= 59)
1 2 3 4 5 | a=10 b=(5*a--)+a console.log(b); //59 |
Reference
Read More
- JavaScript Tutorial
- JavaScript Operators
- Arithmetic Operators
- Unary plus (+) & Unary minus (-)
- Increment & Decrement Operators
- Comparison or Relational Operators
- Strict Equality & Loose Equality Checker
- Ternary Conditional Operator
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Nullish Coalescing Operator
- Comma Operator
- Operator Precedence
i believe you have made a little mistake in your post and prefix example.
postfix is a–, but you have it as –aa
postfix means the operator comes after the variable.
prefix is –a
Thanks Updated the post