🔺 Unary Operators — Pre & Post Increment / Decrement

Last Updated: 14th October 2025


Unary operators work on a single operand (one value).
Common unary operators for counters are:

  • ++ → Increment by 1
  • -- → Decrement by 1

These can be used in two forms:

  • Prefix (pre)++x or --x
  • Postfix (post)x++ or x--

Hinglish Tip 🗣: Unary ka matlab ek hi value par kaam — ++ se value ek badh jaati hai, -- se ek kam.


✏ Pre-Increment / Pre-Decrement

  • Pre form changes the variable first, then returns the new value.
let a = 5;
console.log(++a); // increments a to 6, then prints 6
console.log(a); // 6

let b = 5;
console.log(--b); // decrements b to 4, then prints 4
console.log(b); // 4

✏ Post-Increment / Post-Decrement

  • Post form changes the variable last, then returns the old value.
let x = 5;
console.log(x++); // prints 5 (old value), then x becomes 6
console.log(x); // 6

let y = 5;
console.log(y--); // prints 5 (old value), then y becomes 4
console.log(y); // 4

🧠 Why difference matters

When used inside expressions the position affects the result:

let i = 2;
let a = i++ + 5; // a = 2 + 5 = 7, then i becomes 3
console.log(a, i); // 7 3

let j = 2;
let b = ++j + 5; // j becomes 3, then b = 3 + 5 = 8
console.log(b, j); // 8 3

Hinglish Tip 🗣: Agar expression me current value chahiye pehle, tab x++; agar updated value chahiye tab ++x.


⚠️ Use with care

  • Avoid complex expressions with multiple increments in one line — can become confusing.
  • Prefer clear, separate statements for readability.