Understanding Uninitialized Variables: don't understand a = a + 1

Tell us what’s happening:
I completed the section initializing variables a, b, and c without problem. What I don’t understand is the code under // Do not change code below this line.

How does a = a + 1 work? or b = b + 5? I’m interpreting it as a math equation (“a” could not equal “a + 1”). What is this for and what am I missing?

Your code so far

// Initialize these three variables
var a = 5;
var b = 10;
var c = "I am a";

// Do not change code below this line

a = a + 1;
b = b + 5;
c = c + " String!";

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:

a = a + 1; is “set the value of a to be the current value of a plus 1.”

Variables in programming aren’t quite the same as variables in math in that they do not represent a specific value.

ETA: There is a concept of a variable that represents one specific value. These are called “constants”.

1 Like

Adding to what @ArielLeslie said, the = operator is the assignment operator. Whatever is on the right side of it gets assigned to the left side.

a = a + 1; // means assign the value of a + 1 back to a

The == and === operators are comparison operators. Let’s say you had the following:

var a = 7;
a === a + 1; // false

The expression a === a + 1 means 7 is strictly equal to 7 + 1 (8) , which is definitely false because 7 is not equal to 8.


Thanks @ArielLeslie and @RandellDawson - that makes sense now. Appreciate the help!

Also, just to test you understand the concepts we have explained, I can add a little more complexity to the previous example (as seen below).

var a = 7;
a = a === a + 1;

What do you think the value of the variable a would be after the above code executes and why?

The var a = 7; returns a value of 7.
a = a + 1; would return 8 because the code is telling the computer to complete the equation a + 1 and assign the new value to a.
a === a + 1; says the value is expressly 8, which is false because it was initialized at 7.

a = a === a + 1; still false. you are just making a false statement equal the var a.

Am I understanding that correctly?

Just to clarify what you wrote above, what is the final value of a?

The final value of a would still be 7.

No, the final value of a is false. When you use the assignment operator, I said what is on the right gets assigned to the left. Before the assignment takes place, everything on the right side gets evaluated first. So, in the case of:

a = a === a + 1;

The a === a + 1 gets evaluated first before being assigned back to a.

This is a bit of extra-credit because it requires you to understand associativity. The assignment operator is right-associative, meaning that everything on the right of the = is evaluated before it is assigned to the variable.


Because of associativity, the a = a === a + 1; the false statement is being assigned to a. Therefore, a = false.

You understand perfectly now. Way to go! You have learned much more than you bargained for when you first asked a question.

1 Like

Absolutely! Thank you for the help!

Thank you @ArielLeslie :slight_smile: