# Basic question!

Hello all! I’m (very) new to coding, so bear with me.

I’m currently doing the JAVASCRIPT course, and i’m stuck on this:

// Setup

var a;

var b = 2;

// Only change code below this line

a = 7;

b = a;

Whenever i put a = b; instead of b = a; the code doesn’t work and i can’t figure out why. I’m also confused that - although b was defined as = 2, when a = 7 and b = a, it seems b has values both 2 and 7?

Any help greatly appreciated. Thanks!

Matt

Hi @matthurry. Welcome to FCC. In programming ,`=` is an assignment operator. When you say `a = b`, that can be interpreted as get the value of `variable b` and assign it to variable `a`. Similarly `b = a` is interpreted as get the value of variable `a`, assign it to variable `b`. Therefore `a = b` and `b = a` are not the same. That is why your test is failing.

What is happening above?

1. `var a` . You have declared a variable called `a` but not yet assigned it a value.
2. `var b = 2` .Here you have declared variable `b` and assigned it a value of `2` (assigning value to a variable is also referred to as initializing the variable).
3. `a = 7`. Here you are assigning variable `a` the value `7` (initializing it with the value 7).
4. `b = a`. Interpreted as getting the value of variable `a` (which is 7) and assigning it to variable `b`. Making `b` to evaluate to `7` just like `a`(Remember `b` was initialized to 2).
5. However if you are to say `a = b`. That is interpreted as get the value of `b` which is `2` assign it to `a` so that `a` also evaluates to `2`.

HAPPY CODING

3 Likes

Hi nibble, thanks for answering so quickly.

Yep that’s great - thanks for that. It did explain that x = y statements are read from right to left, but i was still reading them at mathematical statements (where of course x = y and y = x are the same).

Just a quick follow up if you don’t mind:

Is the line var b = 2; now redundant? Because of course, we know that b = 7. Since i have redefined it as 7, can i leave this statement that initialises b as 2?

Thanks again,
Matt

1 Like

For a variable to be assigned a value, it must be declared. For example:

``````var a;  //Declaration of variable a
a = 1; //Assigning value 1 to a
``````

OR
You declare and assign a value to a variable concurrently. Like

`````` var a = 1; //declare variable a and assign value 1 to it immediately
``````

NO. That line is not redundant because it is where `b` was declared and immediately assigned a value (2). If you remove it, you would be using `b` without declaring it (which is UNFORGIVABLE SIN when programming using JavaScript- I don’t know about other programming languages). Good programming practice in Javascript dictates that you should NEVER assign a value to a variable which you haven’t declared.

EDIT

Though not recommended, even if you remove the line `var b = 2` the code will still work in this particular case (In some it will raise an error).
WHY ? You will answer that in more advanced sections of Javascript. Just learn the basics for now.
NB: IT WILL FAIL THE TEST HERE ON FCC if you do that.

it is above the line that say `// Only change code below this line`
so, no, you can’t change it.

in real projects you would declare the variable to something, then use it, then change it to something else. But this is a practice exercise, where you see small examples, which often you would not see in real code.

1 Like