 # Why would we use a mathematical assignment operator?

Hi anyone out there!

Just with this topic, why do you need to use the mathematical assignment operators with the equals symbol i.e. +=, /= if you get the same output with just using the + or / ? I’m going out on a limb and thinking it means something a little later on.

Thanks!

1 Like

It’s harder to answer to a `why?` question than a `how?` Often beginners want to know the reason before the mechanics which only muddies the waters. Syntax and structure are better learned by rote, at the start, then gradually the reasoning reveals itself.

39 Likes

Currently trying to get the hang of the different mathematical notations, such as `-=`, or `/=`.
Does `y *= 2;` have any advantages over `y = y*2`, like script preformance? Or is it just a preference?

The latter feels more natural to me, but I’m interested to know if the former has any benefits in the long run.

3 Likes

It could be it was derived out of preference since the logic could be so easily abstracted. The longer form is still taking place in the background since it is a two step process.

3 Likes

I think you just wanted to pull this out of your “sayings” bag. The question could’ve still been answered and you could’ve still given your life lesson.

8 Likes

In retrospect, you may be right. Keep trolling. You’re sure to find more of the same if you look hard enough.

4 Likes

RIGHT?!?! Makes me not want to ask questions if Homeboy McGenius over here is just gonna take a huge dump on beginners!

6 Likes

One fails to see how any of that is ‘taking a huge dump on beginners’. We try our best to understand what level of learning a member might have and speak to that level while also respecting their intelligence. Being perceived as talking down to someone, and actually doing it are two different interpretations.

6 Likes

Personally, I prefer using the mathematical assignment operators( +=; -=; *=; /=) than math operators( +; -; *; /). Let’s take a look at this example:

Ex1: //all assigning values to variables
let x = 5;
x += 1;
y = 12;
z = 38;

Ex2: //all assigning values to variables
let x = 5;
x = x + 1;
y = 12;
z = 38;

Why I prefer Ex1? There are 2 reasons:

1. If am thinking about doing the math, they’re about to be the same, so it doesn’t matter which one I should choose.
2. If am thinking about the purpose of assigning a value to variable x, then x += 1; makes more sense to me instead of x = x + 1;.
Imagine that I am assigning many values to variables continuously. Then what I want is to make my code clear that this block of code is only about assigning values, NOT suddenly adding in some random math operator.
I hope it’ll help 16 Likes

so basically, don’t worry about that right now. got it. 3 Likes

Does helps to improve the efficiency of the code?

Not if we consider they still require two steps, which is no different from the more formal statement…

``n = n + 1``

What I decided during the exercises is that its much faster than typing it out in y = x*2 form when the variable is a word rather than a letter.

E.g. in one of the exercises, the variable was called levelUp.

Notice how much faster and easier it is to write it this way:
levelUp *= 2

rather than this way:
levelUp = levelUp * 2

So my thinking is that its a shortcut.

3 Likes

I respectfully disagree sir. Knowing the reason i’m going to be needing to know this makes me more confident in processing the information given to me. No idea why but I have a much easier time learning something if I know why im going to need to know it.

3 Likes

That is good to hear! Makes the learning process less stressful!

1 Like

I loved this quote !!

1 Like

Thank you for this answer! I was struggling with the ‘why’, as I learn best if I know the reason I’m doing something (coming from years in CSS). This so clearly answered the why, much appreciated in spelling it out like this!

Hi mister.You look so kind and friendly.Love your beard haha