Need explanation why --mayVar not myVar--

Hi I be able to solve the problem but need some explaination
why it has to be myVar = --myVar;
instead of
myVar –

the position dose matter and seem conflict with i= i–

Thanks!
Your code so far


let myVar = 11;

// Only change code below this line
myVar = --myVar;

console.log(myVar);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36

Challenge: Decrement a Number with JavaScript

Link to the challenge:

There’s difference how -- behave, depending if it’s as a prefix (--i) or suffix (i--). Not always this difference matters.

--i decrements i and returns value which i has after the decrement
i-- decrements i, but returns value which i had before decrement

When used without assignment it might not matter which is used.

let i = 5;
let j = 5;

i--;
--j;

console.log(i);  // 4
console.log(j);  // 4

However, when -- operator is used for example during assignment, this can matter.

let i = 5;
let j = 5;

i = i--;
j = --j;

console.log(i);  // 5
console.log(j);  // 4
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.