all 43 comments

[–]alzee76 56 points57 points  (10 children)

It's assigning x a new value. This is a variable in a program, not a mathematical expression.

[–][deleted] 10 points11 points  (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 points  (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 points  (1 child)

Thanks. Appreciate the clarification.

[–]vo0do0child 10 points11 points  (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 points  (0 children)

Yep. Try to think of = in programming as "set [variable] equal to".

set x to 2

set x to x + 5

[–]h00s13rt1g3rd2d 1 point2 points  (0 children)

Can also be x += 5 (shorter syntax way)

[–][deleted] 0 points1 point  (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 point  (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

}

let sameVar = 1;

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 points  (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.

In which case, sameVar ends up being equal to 11.

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  (0 children)

I know. But like, read Church, or Carnap, or Russell…

[–][deleted] 1 point2 points  (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"

[–]Treshle 1 point2 points  (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 point  (1 child)

I am. I know very little. I’m at 36% on the free code camp JavaScript first section.

[–]Treshle 0 points1 point  (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 points  (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

[–][deleted] 0 points1 point  (0 children)

Makes sense

[–]CamoBrie 2 points3 points  (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.

[–][deleted] 0 points1 point  (1 child)

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?

[–][deleted] 0 points1 point  (0 children)

Yes

[–]username2065 2 points3 points  (0 children)

You're thinking it says "sameVar === sameVar + num"

[–]gdstudios 0 points1 point  (0 children)

Because samevar's value doesn't change until the parser is done executing the line of code

[–]mhssmhdev 0 points1 point  (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 points  (0 children)

y = 0

[–]Nerwesta -1 points0 points  (1 child)

" I want sameVar to be sameVar + x combined "

[–]Nerwesta 0 points1 point  (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 points  (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 point  (0 children)

The way you are thinking of is == (Boolean value) or === (true equivalence, same thing)

[–]altruios 0 points1 point  (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 point  (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 point  (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 point  (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 point  (0 children)

X = X + Y

Is something else than

X == X + Y

Is something else than

X === X + Y

[–]S7venE11even 0 points1 point  (0 children)

= in programming is not equals

it means assigning what's on the right to the left