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:
https://www.freecodecamp.org/challenges/understanding-uninitialized-variables

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

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

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?

The final value of a would still be 7.

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.

3 Likes

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

Absolutely! Thank you for the help!

Thank you @ArielLeslie :slight_smile: