Storing Values with the Assignment Operator misunderstanding

Tell us what’s happening:

I do not understand how to complete this problem. Also why cant I put my code above line 5? This is kind of vague for newbies like me ii dont understand what i am doing wrong, i even tried watching the youtube video by Beau.

Your code so far


// Setup
var a;
var b = 2;

// Only change code below this line
var a = 7;
var b = var a;

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator

When you declare a variable using the var command, that is kind of like labelling an empty box. You can not have two boxes with the same label as you have done.
To assign a value to a variable, just treat it like you would a math equation like y=3 would mean the variable y has the value 3.
So remove the extra vars and make sure you put the variable that is getting the value on the left if the equals . Values on right.

1 Like

Thank you for replying, but I am still a bit confused. How do I have 2 boxes with the same label? I understand y= 3, but shouldnt var b = var a; “Assign the contents of a to variable b” and shouldnt var a = 7; “Assign the value 7 to variable a”?

2 tasks:
1.Assign the value 7 to variable a
2. Assign the contents of a to variable b

You are confusing declaration of a variable vs. assignment of one variable’s value to another variable.

The declaration of a variable happens when var is used before it. You can write:

var a;

and you are declaring it. At this point a is “defined”. After a variable is declared using var, const, or let you do not use the var, const, or let in front of it again with using the variable. It is a one-time thing.

If I write:

var a = 6;

then I am doing two things. 1) I am declaring the variable a and 2) I am assigning a value of 6. This is known as initialization of a variable.

So look at your original code below:

// Setup
var a;
var b = 2;

// Only change code below this line
var a = 7;
var b = var a;

There is no need to put the var before a when assigning it the value 7. Also, the var is not needed in front of b when assigning variable a to it. Finally, you definitely do not want to put the var in front of the a being assigned to the variable b.

I hope this clears up a few things for you. If not, let us know.

1 Like

Wow the first aswer made me go back to the last challenge, your right i was totally ignorant on what declaring a variable was thank you very very much! Now i get why the code must be written AFTER line 5 :D.
Now to read the rest of your answer…

-@ var a= 6: nice i didnt know i was doing 2 things there it looked like 1 just defining the variable thanks for clarifying what initializing a variable is.

-@ so look at your original code below: When I looked at the code before reading your last paragraph my code still semi made sense to me . Then i read the last paragraph and everything click like tetris. Thank you!!!

If var a; is called a “declaration”, does that mean b = a; is called a regular “operation”? Or what do we call these things(statements?) in javascript we are doing now. Like in html i write div tags, in css we write rulesets, what is this called in javascript?

That is called an assignment statement, because you are assigning a value to a variable. In this case the variable on the right side is the value. A statement in JavaScript is just some syntax structure which expresses some action to take place.

1 Like

Thanks a million times!