Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Increment & Decrement Operators in JavaScript

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.

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.

                            
let x=10
++x;     //increment the value by one  x=x+1
console.log(x);   //11
 
                     
                            
                        
                            
let x=10
x--;     //decrement the value by one  x=x-1
console.log(x);   //9
                     
                            
                        

Example of a decrement Operator.

                            
let x=10
--x;     //decrement the value by one  x=x*1
console.log(x);   //9
                     
                            
                        
                            
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

                            
let a=10;
++a;
console.log(a)   // 11
--a;
console.log(a)   // 10
                     
                            
                        

Postfix Example

                            
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

  1. The value of the variable a is incremented by 1
  2. Then it returns the value.
        
a=10;
b=++a;      //a is incremented, a is then assigned to b
console.log(b);  //11
console.log(a);  //11
 
 
        
    
                     
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++,

  1. The value of the variable is returned.
  2. Then the variable a is incremented by 1
                     
a=10;
b=a++;  //a is assigned to b, then a is incremented
console.log(b);  //10
console.log(a);  //11
 
 
 
        
    
                      
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.

                            
a=10;
b=5*--a; 
console.log(b);  //45
                     
                            
                        

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)

                            
a=10
b=(5*a--)+a      
console.log(b);  //59