Complete beginner here, possible stupid question

So, I’m only on like the second Javascript lesson. It’s teaching me how to assign values to variables.

// Setup

var a;

var b = 2;

// Only change code below this line

a = 7;

b = a;

This is the correct solution, but I’m confused on the logic here. If there is a Variable a, and it equals 7, and variable b equals 2. How does variable b equal a? What will this do to future code? If I go to use variable b in the future, will it be 2 or 7, and why?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


The answer is that you replaced the value of b.

Javascript is loaded from top to bottom. So when you wrote b = a; you changed b’s value to be a not 2. So in the browser b will = 2 until it hit’s b = a and b will no longer = 2.

So in essence, it’s letting me re-use variable b in the future without having to create a new variable? All of my code above b = a will have b = 2, whilst everything below will have b = 7?

No quite. Once the code loads, b will equal a. You can’t go back to 2 without declaring it again or having the code change it back.

So what is the point of this exercise? Is there an example of where this would be more useful than just creating a new variable to express whatever value you’re looking to express? Why would I create variable a=7 and b=2, just to replace b with a? This is where I have trouble learning programming, I don’t fully understand the logic/uses of things. It seems pointless.

I’m struggling with the same thing. I just keep plowing ahead, hoping it’ll eventually click

The last line of the code will assign the value of a (7) to b, making b = 7.
However, when interpreting JS code, don’t get confused with equality == or === and assignment =.

1 Like

This shows you two things. 1) the order of how JS is read by the browser. And 2)that you can change variables. There will be times when you need to change a variable and when you may need to pull data from other variables, this shows you that is possible.

For example. Say you have created a CSS generator. The user puts in data, and it spits out css code. You need to be able to change the variable as the user changes the data they input.

You don’t want a new variable everytime. because you won’t want to write a ton of extra code to call new variables for every single thing…this becomes bloated and hard to maintain.

There are a lot of ways to do this. And you generally won’t change the variable just cause in an actual app. This is just the basic concept change. You’ll get it. It takes time.

Every time you use the assignment operator (=) you are changing the value of a variable.

var something = 1; // something is now 1
something = 2; // something is now 2
something = 'bananas'; // something is now 'bananas'

“a = 7” and “b = a” might look like a mathematical statement, but in programming, these statements are interpreted differently.

First, in programming, statements like these are read right to left, and NOT left to right. The expression on the right side of the equals sign is evaluated first. Then it’s stored in the variable on the left of the equals sign.

Second, the equals sign is called an assignment operator. It’s not the mathematical “something equals another” kind of thing. The assignment operator takes the value on the right side and stores it in the variable referenced on the left side—setting a value, so to speak.

a = 7 should be read as saying: Set a value of 7 to the variable called ‘a’.
b = a should be read as saying: Copy the most recent value of ‘a’ to the variable called ‘b’. This is why 7 ends up in ‘b’.

You can consider this as sort of a contrived exercise. In this example, you won’t see the purpose of doing something like this. But in real-word scenarios, you might need to copy the value of one variable to another in certain cases.


Yo! Thanks. That makes so much sense! I’ve been reading it as mathematical logic. When I don’t think of it that way it is much more palatable.