Member-only story

Increment and Decrement Operators in JavaScript

Pravin M
3 min readJul 27, 2024

--

Increment and Decrement Operators in JavaScript

For more questions and answers visit our website at Frontend Interview Questions

Increment Operator (++)

The increment operator (++) increases the value of a variable by one. It can be used in two forms:

  1. Postfix Increment (i++): Increments the value after returning the original value.
  2. Prefix Increment (++i): Increments the value before returning the new value.

Example:

let x = 5;
let y = x++; // y is assigned the value of x (5), then x is incremented to 6
console.log(x); // Output: 6
console.log(y); // Output: 5
let a = 5;
let b = ++a; // a is incremented to 6, then b is assigned the value of a (6)
console.log(a); // Output: 6
console.log(b); // Output: 6

In this example, x++ returns the original value of x before incrementing it, whereas ++a increments a before returning the new value.

Decrement Operator ( — )

The decrement operator (--) decreases the value of a variable by one. It also has two forms:

  1. Postfix Decrement (i--): Decrements the value after returning the original value.
  2. Prefix Decrement (--i): Decrements the value before returning the new value.

--

--

Pravin M
Pravin M

Written by Pravin M

I am a frontend developer with 10+ years of experience Blog :- https://www.frontendinterviewquestions.com

No responses yet