How to understand this..?

i know that “n=n+1” his also equal to “n+=1” but i don’t now how to understand it help me to understand this please…

n += 1 is shorthand for n = n + 1. It’s a common thing to want to do. So there’s shorthand for it so that it’s easier to write. There isn’t anything more to it than that.

yes, i know that but what is meant by “+=” that is written why not this"=+"…???

yes, i know that but what is meant by “+=” that is written why not this"=+"…???

Because the writers of JS decided to use += and not =+. They inherited that from older languages. If we write our own language, we could use =+ or whatever we want.

There also may be a practical reason. In JS, there is the unary plus operator (+) that converts to a number. That could create ambiguity. If I have:

const numString = "127";
let num = 42;

num =+ numString; // Our new operator - What would it mean?

What would that last line mean?

Would it mean:

num = num + numString: // "42127"


num = Number(numString); // 127

Don’t worry too much about the why or arbitrary language decisions. There is often a history there. I would worry more about just learning what the language is, and how it works.


Remember that = is the assignment operator and does not represent equality.
What we are doing when we use = is putting a new value into a variable.
In the case of n = n + 1, it is changing the value of n to hold its old value plus one.

let n = 2;
console.log(n); // 2
n = n + 1;
console.log(n); // 3
n += 1;
console.log(n); // 4

Thank you Mr.Kevin.smith it helps me a lot…

Thank you ArielLeslie for your help…

we need to flip the signs because =+ already has a use:

var n=5  //we create var 'n' and make it equal 5
n=+3  //we change the value of n, its now 3
n=-2  //we change the value of n, its now -2
n+=5  //this time, we dont change the value to 5, we add 5 to the existing value
n-=3  //here we subtract 3 from the existing value
n=-3  //here we make n be equal -3

do you see why we cant use =+ to add, or =- to subtract from the existing value? There already is operation assigned to this expression. And since its common practice to make some calculations with the current value of a variable, the JS creators came up with the obvious shorthand expression

1 Like

Ah yeah, this is the reason why @muhammadusman75rb

Space has no meaning in JS, space is only added to code to make it easier for humans to read.

n =+ 1 is exactly the same as n = +1 is exactly the same as n = + 1 is exactly the same as n=+1.

It’s easier to have the + on the other side, then if you’re designing the language you don’t need elaborate rules to allow the computer to figure out that the programmer wanted to do that particular thing


Don’t ever write n += 1 or n =+ 1… just write n = n + 1 . It’s much easier to understand and you’ll save yourself a lot of trouble.

I’m sorry, but I disagree with this. Anyone with coding experience understands +=. I agree that we shouldn’t write n =+ 1, though I might write n = +x if I wanted to coerce x to a number.

By your logic I shouldn’t use arrow functions or destructuring or rest operators because they confuse beginners. I’ve worked with developers that didn’t understand how the reduce method works - but I’m not going to stop using it. My audience is made up of experienced developers so I assume good knowledge of JS. As it goes, += is not particularly confusing or obscure (no shade to the OP).

I agree that we shouldn’t use arcane notation that creates a comprehension puzzle, but the += is used in many computer languages and is a standard operator in JS. Personally, I think n += 1 is slightly easier to read than n = n + 1 - there is one less symbol to mentally process. And of course n++ is easiest of all - I look at it and I instantly know what it is doing.

And I think if you start telling other devs that “I don’t think we should use += because it might confuse people” … I think you are going to get some rolled eyes.

For the record, I’ve never encountered a working dev that was even slightly confused by +=. (Again, no insult to the OP - we all start somewhere.)


It’s the type of shorthand that it might be good to avoid while you’re unfamiliar with reading and writing code, but += is a convention that you’ll get pretty used to over time and eventually probably prefer. If you ever end up working with a team of developers this type of convention is the sort of thing that will need to be part of a shared agreement, one way or the other.


agreement += 1

I am all for avoiding unreasonably terse syntax, but += is pretty common across many languages. You’ll want to conform to the conventions for whatever project you are a part of.

1 Like

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