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.

69 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.

4 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.

10 Likes

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

8 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.

19 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.
    This may help you when you code for pages already and want to take a look back for reviewing your thought process.
    I hope it’ll help :slight_smile:
27 Likes

so basically, don’t worry about that right now. got it. :laughing:

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.

6 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.

5 Likes

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

1 Like

I loved this quote !!

2 Likes

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

I can’t see what @mtf wrote that downplays your intelligence. He simply made a point - my view.

I’m not an expert and I’ve only just started learning so my answer is in simple terms. but i think the original question was to do with when would we use a mathematical operator. My logic says when you want to change the value of a variable based on a calculation. Maybe a better question is why would you use it?

My suggestion would be this forum for example, every time someone replies to a question, a counter tells you how many replies. So I’m assuming that the the counter waits for a click on the reply button and when it hears one, the code does something like this

reply = reply + 1;

or

reply += 1;

Another use might be in calculating score in a game.

For example if you hit the red ball worth 5 points then something like an if statement could be use and within that you would have

score +=5;

A different if statement might be used for the blue ball which scores 10 points in which case that statement would have within it

score =+ 10;

In my imaginary game you have to avoid hitting the butterflies but if you do hit then another if statement would have within it

score -=50;

Let’s say when you reach a score of zero or less, it’s game over.

Anyway, that’s how I see a use for them.