JavaScript ++ and --

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

++ adds one; -- subtracts one. Prefix (++x) updates then yields the new value. Postfix (x++) yields the old value then updates.

JavaScript Syntax

JavaScript
let n = 1;
console.log(n++);
console.log(n);
console.log(++n);
Try it Editor

How It Works

  1. Postfix. n++ returns 1, then n becomes 2.
  2. Prefix. ++n makes 3 and returns 3.

Examples

Example 1 — In a for loop

Classic counter.

JavaScript
for (let i = 0; i < 3; i++) console.log(i);
Try it Editor

How It Works

  1. i++ runs after each body.

Example 2 — Decrement

-- counts down.

JavaScript
let n = 3;
console.log(--n);
Try it Editor

How It Works

  1. Prefix decrement returns the new value.

Example 3 — Prefer += 1 when unclear

Readability tip.

JavaScript
let n = 0;
n += 1;
console.log(n);
Try it Editor

How It Works

  1. Same effect as ++ without prefix/postfix confusion.

Frequently Asked Questions

Prefer standalone updates or += when the value of the expression matters.
Yes for BigInt variables: n++ where n is a BigInt.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful