Storing Values with the Assignment Operator

I can’t understand how this works.

1 Like

eg if you have a variable called name
eg var name;
well at the moment nothing is assigned to it … then you …
name = 'John’
well now the string 'John is assigned to name …
if you console.log(‘name’) it shows John in your console.
well if you create another variable eg var myName;
same thing you have a variable but nothing assigned to it … if you then …
myName = name … well it now gets whatever is assigned to name and it also assigns it to myName …
so when you console.log(myName) you get John
you can then do … myName = 'Pat’
console.log(myName) you get Pat
console.log(name) you get John

1 Like

"In JavaScript, you can store a value in a variable with the assignment operator.

myVariable = 5;

Assigns the Number value 5 to myVariable.

Assignment always goes from right to left. Everything to the right of the = operator is resolved before the value is assigned to the variable to the left of the operator.

myVar = 5;
myNum = myVar;
Assigns 5 to myVar and then resolves myVar to 5 again and assigns it to myNum.

Assign the value 7 to variable a.

Assign the contents of a to variable b."


In JavaScript, assiging a variable looks like this:

myVariable = 'value';

Variables are often assigned at the same time as they are initialized, so you’ll often see something more like this:

var myVariable = 'value';

However, in the challenge code, the variables have already been initialized so you don’t need to use var.

Note that once it has been assigned, a variable stores the value it was given. This means you can assign another variable to the same value indirectly:

var myVar1, myVar2;

myVar1 = 9000;
myVar2 = myVar1; // After this line executes, myVar2 is also equal to 9000.


I need help, pls … I don’t seem to hack the 1st step

Dogs can’t understand how this works. You can, you just don’t… yet. So, which part are you not understanding? Can you show us what you’ve tried so far?

Ever seen a dog bury a bone - and dig it up later?

Dogs understand how this works


// Setup
var a;
var b = 2;

// Only change code below this line.
var a, b;

var a = 7;
var a = b;

The “a should be assigned to b with =” though

1 Like

answer here
var a = 7;
var b = a;


A post was split to a new topic: Given answer does not work for Storing Values with the Assignment Operator challenge

This one worked.
Many thanks!

Wrong. It’s the other way round.

If you assign the content of a to variable b, it is:
var b = a;