# how to assign a val

Tell us what’s happening:

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

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36`.

Challenge: Assigning the Value of One Variable to Another

Welcome,

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

The more information you give us, the more likely we are to be able to help.

Do you understand what the given sample code is doing?

``````var myVar;
myVar = 5;
var myNum;
myNum = myVar;
``````

The starter code is

``````var a;
a = 7;
var b;
``````

and the task is to assign the value of `a` to `b`. You wrote

``````b = 7;
``````

The task is not to assign a fixed value 7 to b. The task is whatever the value of a is, assign that value to b.

What are you trying to achieve with this?

``````var ab;
``````

Variables `a` and `b` are declared and `a` has a value assigned to it. You assign that value of `a` to `b`. Don’t add any unnecessary code. Study the sample code

``````var myVar;
myVar = 5;
var myNum;
myNum = myVar;
``````
1 Like

Tell us what’s happening:

``````
// Setup

var a;

a = 7;

var b;

b=7

var ab; b=7
a=b;

// Only change code below this line

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36`.

Challenge: Assigning the Value of One Variable to Another

We can assign a value 8 to a variable x like this.

``````var x = 8;
``````

We can declare the variable first without assigning any value, and then assign a value to it like this.

``````var x;
x = 8;
``````

The next code assigns a value of 8 to a variable x and then assigns a new value 12 to x.

``````let x = 8;
x = 12;
``````

Can you write code to assign the value of 8 to a variable x and then assigns a new value of 20?
Now, consider the following code.

``````var y = 20;
var x;
x = y;
``````

The value of y is 20. What do you think the value of x would be?

``````var ab; //Don't declare a variable you don't assign
// any value to it. Just because there are
// variables a and b, declaring a new
// variable ab that combines  variables a and b will
//  not do anything. It won't magically create
// a new value that somehow adds two values
//  or assigns one value to another.
// There's no Harry Potter around here.

b = 7     //Terminate statements with a semicolon like b = 7;
``````

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.