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.
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.
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.
“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.