# What is the difference between prefix and postfix operators?

JavaScript, Math · Oct 31, 2021

The increment operator (`++`) adds `1` to its operand and returns a value. Similarly, the decrement operator (`--`) subtracts `1` from its operand and returns a value. Both of these operators can be used either prefix (`++i`, `--i`) or postfix (`i++`, `i--`).

If used prefix, the value is incremented/decremented, and the value of the expression is the updated value.

```let i = 0;    // i = 0
let j = ++i;  // i = 1, j = 1
let k = --i;  // i = 0, k = 0```

If used postfix, the value is incremented/decremented, and the value of the expression is the original value.

```let i = 0;    // i = 0
let j = i++;  // i = 1, j = 0
let k = i--;  // i = 0, k = 1```

### Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub.

## More like this

• ### JavaScript Interview Questions

Prepare for your next JavaScript interview with this collection of interview questions and answers.

Collection · 33 snippets

• ### Integer to roman numeral

Converts an integer to its roman numeral representation. Accepts value between `1` and `3999` (both inclusive).

JavaScript, Math · Oct 22, 2020

• ### How can I remove trailing zeros from a number in JavaScript?

When formatting decimal values in JavaScript, trailing zeros can be undesired. Here's how to deal with them.

JavaScript, Math · May 8, 2022

• ### Hamming distance

Calculates the Hamming distance between two values.

JavaScript, Math · Dec 28, 2020