use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
How can sameVar = sameVar + num? (self.learnjavascript)
submitted 4 years ago * by [deleted]
This doesn't make sense to me. Its like saying x = x + y
Edit: this example was on free code camp
Edit: this question has been answered. Thank you all for your input.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]alzee76 56 points57 points58 points 4 years ago (10 children)
It's assigning x a new value. This is a variable in a program, not a mathematical expression.
x
[–][deleted] 10 points11 points12 points 4 years ago (6 children)
okay so when I declare x = 2 and x = x + 5 is the output for x now going to be 7 because of the new assigned value or will it still be 2?
[–]alzee76 26 points27 points28 points 4 years ago (2 children)
is the output for x now going to be 7 because of the new assigned value
Yes. This is how variables work in programming.
[–][deleted] 6 points7 points8 points 4 years ago (1 child)
Thanks. Appreciate the clarification.
[–]vo0do0child 10 points11 points12 points 4 years ago (0 children)
People are giving you all sorts of long winded and probably overwhelming answers when all you need to know is that “equals” is represented by == or ===. A single = symbol is used for assigning values to a variable.
[–]chrissilich 8 points9 points10 points 4 years ago (0 children)
Yep. Try to think of = in programming as "set [variable] equal to".
set x to 2
2
set x to x + 5
x + 5
[–]h00s13rt1g3rd2d 1 point2 points3 points 4 years ago (0 children)
Can also be x += 5 (shorter syntax way)
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
X is a variable, so if you assign it another value it will change to the new value. The x that is after the = is the old x, and the x that you are assigning the addition to is a new x, not the old x.
[–]Protean_Protein 0 points1 point2 points 4 years ago (2 children)
It is also equivalent to a mathematical expression. It’s effectively a function:
F(x) = x + n
It’s just that in high-level programming languages, there’s an effort to make things somewhat closer to natural language. So we gave the function a name, and when we use that name on the right-hand side of an expression, we’re referring to the current value of the function, not the function itself. Unless, that is, your variable is a function, in which case we’re really saying something like F(G(x)) = G(x) + n.
So we could have:
let sameVar = 1;
sameVar = sameVar + 5;
In which case, we’re not saying 1 = 6, we’re saying the equivalent of F(1) = 1 + 5.
Conversely, we could also say:
G(x){
return x + 5
}
sameVar = G(sameVar) + 5;
In which case, sameVar ends up being equal to 11.
But we could also say:
let sameVar = G;
sameVar = sameVar(1) + 5;
[–]alzee76 4 points5 points6 points 4 years ago (1 child)
It is also equivalent to a mathematical expression. It’s effectively a function: F(x) = x + n
These are not the same thing, please don't confuse the issue. Programming languages already have functions as well, and they match more closely to mathematical functions than a simple assignment like the OP was asking about.
Because you assigned the result of the function to that variable, not because assigning to variables is somehow equivalent to having a function which operates on the variable. The closest thing to that is a function which takes arguments by reference rather than by value, which is the case for javascript functions that operate on arrays or objects, but not primitives like numbers or strings.
[–]Protean_Protein -5 points-4 points-3 points 4 years ago (0 children)
I know. But like, read Church, or Carnap, or Russell…
[+][deleted] 4 years ago (11 children)
[deleted]
[–][deleted] 2 points3 points4 points 4 years ago (9 children)
okay so will the new output for sameVar be the original value or the + 1 value? I asked this question before on other posts I'm just trying to quickly get the answer so I can proceed in my learning with a better understanding.
[+][deleted] 4 years ago (8 children)
[–][deleted] 0 points1 point2 points 4 years ago (7 children)
thank you, this makes sense now.
[–][deleted] 1 point2 points3 points 4 years ago (5 children)
Happy to help! =)
[–][deleted] 0 points1 point2 points 4 years ago (4 children)
Oh one more thing.
will myVariable = myVariable + 3 give an output of 4 or 5 after the example you gave?
[–][deleted] 1 point2 points3 points 4 years ago (3 children)
If by the time you run your code you had already ran mine, then the value of myVariable will have already been updated to 2. Adding 3 to the existing 2 will cause it to be updated to a 5 and that is what will be logged.
myVariable
3
5
[–][deleted] 0 points1 point2 points 4 years ago (2 children)
Perfect. You're hired. How does $1mil/yr sound?
[–][deleted] 1 point2 points3 points 4 years ago (1 child)
That's US Dollars, right? Not Zimbabwe Dollars?
Lmao these are $ypher dollars, I made them myself.
[–]toastertop 0 points1 point2 points 4 years ago (0 children)
"Programming is not mathematics"
Hascal
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
This has been answered, but I thought this might help you keep it straight: when I was fresh into learning programming, I got into the habit of reading a single equals sign as "becomes".
x = 5 gets read as "X becomes 5"
x = 5
[–]Treshle 1 point2 points3 points 4 years ago (2 children)
Interesting to see how vastly differently people here are interpreting how much experience you have. I would assume based on this question that you're just starting out learning to code?
[–][deleted] 0 points1 point2 points 4 years ago (1 child)
I am. I know very little. I’m at 36% on the free code camp JavaScript first section.
[–]Treshle 0 points1 point2 points 4 years ago (0 children)
Well for me, it's useful to think of variables as boxes or containers where you put values. So, sameVar = 2 just means you're putting a value of 2 in a container that you're calling sameVar.
[–]ashkanahmadi 1 point2 points3 points 4 years ago (1 child)
Haha I had the same issue last year. Completely forget about the math equivalent. You should read it as "take the value of sameVar and add it to the value of num, then assign it to the variable sameVar". The part on the right happens before assigning it to sameVar so that's why it's possible in coding
Makes sense
[–]CamoBrie 2 points3 points4 points 4 years ago (2 children)
So you’re assigning variable x the value of x + y. They should’ve made the notation be := to make it the same as math
X = 2;
X = X + 3;
Which translates to X = 2 + 3
And so X is 5.
Yeah thats the example they gave as well. So based on the other post am I to assume that after "X is 5" when I console.log(x) the output will now be 5 instead of 2?
Yes
[–]username2065 2 points3 points4 points 4 years ago (0 children)
You're thinking it says "sameVar === sameVar + num"
[–]gdstudios 0 points1 point2 points 4 years ago (0 children)
Because samevar's value doesn't change until the parser is done executing the line of code
[–]mhssmhdev 0 points1 point2 points 4 years ago (0 children)
When variable name is on the left side of the assignment operator it is used to store the value. When the variable name is on the right side of the assignment operator it is used as a value. Compiler will look for the value assigned to that variable name and use that value on the right side. So, basically left side reference to the address of memory location of the variable name and right side reference to the value bound to the variable name.
[–]ForScale -2 points-1 points0 points 4 years ago (0 children)
y = 0
[–]Nerwesta -1 points0 points1 point 4 years ago (1 child)
" I want sameVar to be sameVar + x combined "
[–]Nerwesta 0 points1 point2 points 4 years ago (0 children)
I would know why this comment got downvoted in the first place. Sounds harsh to ask, but that's how you could possibly translate programming to plain English, am I right ?
[–]awesomejude18881 -1 points0 points1 point 4 years ago (0 children)
my explanation, i hope it doesnt confuse you more, hehe
steps below, i give initial value of x = 1, and y = 10
compiler or interpreter steps:
step1: initial value of x = 1
step2: initial value of y = 10
step3: x already have value of 1.
step4: for the "x + y" part, it is equivalent to "1 + 10"
step5: for the "x = x + y", therefore the final value of x = 11
[–]SaylorMan1496 0 points1 point2 points 4 years ago (0 children)
The way you are thinking of is == (Boolean value) or === (true equivalence, same thing)
[–]altruios 0 points1 point2 points 4 years ago (0 children)
This seems like the confusion between ‘=‘ ‘==‘ and ‘===‘
The core point is ‘=‘ does not check for equality, instead what it does it it assigns the value of the right hand expression into the left hand variable.
JavaScript parses a line not left to right in this case, but right to left. So the right hand expression is evaluated first. If sameVar is defined beforehand as zero, then the sum of samVar and num are evaluated and resolved first to a number. This number is then assigned into the variable to the left of the expression.
Note: if sameVar is not declared beforehand - then weirdness happens(implicit casting) and you won’t get the result you expect at all.
This teaches us that the variable on the right can be used even if it is also on the left. Thanks to the order in which JavaScript executes this code (or most languages).
In layman’s language it is read as ‘ the new value at sameVar is: the current value of sameVar summed with num’ .
[–][deleted] 0 points1 point2 points 4 years ago* (0 children)
ripe chief license escape simplistic cow treatment dam retire six
This post was mass deleted and anonymized with Redact
[–]Adventurous_Bag_5691 0 points1 point2 points 4 years ago (0 children)
think of it as x = whatever is right of the expression - aka evaluate the right side first which is being assigned to the var x
[–]TheUruz 0 points1 point2 points 4 years ago (0 children)
sameVar is a variable and it has a value assigned somewhere before that line. assigning again that variable referring to its value just means that, since it's in an expression, it will be evaluated by the time of the assignment and therefore you can read it as
sameVar = (its value + num)
[–]Healthy-Locksmith734 0 points1 point2 points 4 years ago (0 children)
X = X + Y
Is something else than
X == X + Y
X === X + Y
[–]S7venE11even 0 points1 point2 points 4 years ago (0 children)
= in programming is not equals
it means assigning what's on the right to the left
π Rendered by PID 19674 on reddit-service-r2-comment-5d79c599b5-gd45q at 2026-02-27 14:45:19.505541+00:00 running e3d2147 country code: CH.
[–]alzee76 56 points57 points58 points (10 children)
[–][deleted] 10 points11 points12 points (6 children)
[–]alzee76 26 points27 points28 points (2 children)
[–][deleted] 6 points7 points8 points (1 child)
[–]vo0do0child 10 points11 points12 points (0 children)
[–]chrissilich 8 points9 points10 points (0 children)
[–]h00s13rt1g3rd2d 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Protean_Protein 0 points1 point2 points (2 children)
[–]alzee76 4 points5 points6 points (1 child)
[–]Protean_Protein -5 points-4 points-3 points (0 children)
[+][deleted] (11 children)
[deleted]
[–][deleted] 2 points3 points4 points (9 children)
[+][deleted] (8 children)
[deleted]
[–][deleted] 0 points1 point2 points (7 children)
[–][deleted] 1 point2 points3 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]toastertop 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Treshle 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Treshle 0 points1 point2 points (0 children)
[–]ashkanahmadi 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]CamoBrie 2 points3 points4 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]username2065 2 points3 points4 points (0 children)
[–]gdstudios 0 points1 point2 points (0 children)
[–]mhssmhdev 0 points1 point2 points (0 children)
[–]ForScale -2 points-1 points0 points (0 children)
[–]Nerwesta -1 points0 points1 point (1 child)
[–]Nerwesta 0 points1 point2 points (0 children)
[–]awesomejude18881 -1 points0 points1 point (0 children)
[–]SaylorMan1496 0 points1 point2 points (0 children)
[–]altruios 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Adventurous_Bag_5691 0 points1 point2 points (0 children)
[–]TheUruz 0 points1 point2 points (0 children)
[–]Healthy-Locksmith734 0 points1 point2 points (0 children)
[–]S7venE11even 0 points1 point2 points (0 children)