Files
30-seconds-of-code/blog_posts/javascript-prefix-postfix-operators.md
Isabelle Viktoria Maciohsek 1eaf279c44 Update blog expertise
2022-03-01 21:07:20 +02:00

1.0 KiB

title, type, tags, expertise, author, cover, excerpt, firstSeen
title type tags expertise author cover excerpt firstSeen
What is the difference between prefix and postfix operators? question javascript,math beginner chalarangelo blog_images/plant-candle.jpg While both the prefix and postfix operators increment a value, the resulting value of the expression is very different. 2021-10-31T05:00:00-04:00

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