Outcomes When Using "-" Assignment Operator Versus "-="

Hi All! So I just completed the Dog Years project in the intro to Javascript section and was wondering if someone might be able to help explain two different outcomes I was getting when switching between the “-” and “-=” operators.

When using the code below the myAge variable would change to 26 for the rest of the program. Upon switching to “-” operator however it retained it’s intended number the whole way though.

// The variable of my age, 28, will be assigned the name myAge
let myAge = 28;

// Variable to denote how early years will be impacted by dog years math
let earlyYears = 2;

earlyYears *= 10.5;

// This will impact how later years will be impacted by dog years math
let laterYears = myAge -= 2;

// This will calculate how the laterYears are changed by dog years math
laterYears *= 4;

// Adds up the early years with the  later years to determine my dog age
let myAgeInDogYears = earlyYears + laterYears;

// Variable to save my name in lowercase letters
let myName = 'Sean'.toLowerCase();

// Logs the variables into a sentence explaining my name, age, and dog age
console.log(`My name is ${myName}. I am ${myAge} years old in human years which is ${myAgeInDogYears} years old in dog years`);

I’m really just curious as to why one operator changed this variable while the other did not. Perhaps I’m not totally clear on the difference between the two, but my assumption is this: “-” changes the value in that instance it is being used while “-=” will change it permanently from that point forward.

Is this correct? Or am I misinterpreting what’s happening here? Thanks in advance :slight_smile:

- is a minus operation. It does not happen in place, but needs to be assigned.

a = 43
a = 43 - 1    // acting on the value 43

The in-place operation equivalent is,

a -= 1       // acting on the value referenced by `a`

Awesome! Thanks so much for clarifying.