JavaScript Code


Decrement Operator in JavaScript


Decrement Operator

In JavaScript, Arithmetic Decrement operator takes a variable as operand, decrements the value in the variable by one.

Syntax

-- symbol is used for arithmetic decrement operator

Based on whether the decremented value is reflected in the variable during post executing the statement, or while executing the statement, there are two types of decrement operations: post-decrement, and pre-decrement.

a--  //post-decrement
--a  //pre-decrement 

Examples

1. Decrement value in a by one.

var a = 6;
a--;
console.log(a);

2. Post-decrement value in a by one.

var a = 6;
console.log(a--);
//6
console.log(a);
//5

3. Pre-decrement value in a by one.

var a = 6;
console.log(--a);
//5
console.log(a);
//5