I don't undrstand Decrement opreatore works

Tell us what’s happening:
I don’t understand (–a) or (a–) basically there are same behavior
I write a code

let a =5;
a–;
or
–a;
console.log there all about result 4

Your code so far


let a =5;
a--;
or 
--a;


// Only change code below this line



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36.

Challenge: Decrement a Number with JavaScript

Link to the challenge:

So, the difference between prefix and postfix decrement (or increment) operators is when the operation is executed. As you may guess, when the decrement operator is prefixed the returned value will be the result of the decrement operation and when the operator is postfixed the returned value will be the value prior to the operation.

Let’s see it with code:

let a = 0
let b = ++a // now b = 1 and a = 1 because the operator changed the value of a and then it evaluated the expression
let c = b++ // now c = 1 and b = 2 because the operator only changes the value after the expression is evaluated
console.log(a, b, c) // 1, 2, 1
2 Likes

thank you for detailed things explain