# What would the following value equals to in this code?

• Nov 8, 2010, 15:06
Novice2010
What would the following value equals to in this code?
I have looked at this code and determined I will get the following
if I run individual variable.

x = 10
y = 5
z to 5

How ever I am not sure.

Because when I run the function using document.write

Like this:
Code:

`document.write(get_xyx());`
I get undefined

Here is the code:

Code:

```<script type="text/javascript"> var n=5; var x=n + n; var y=n + n; function get_xyz () { var x = n; y = n; z = n; } get_xyz(); </script>```
Just a little confused.

Novice
• Nov 8, 2010, 15:12
Kalon

Code:

``` function get_xyz () { var x = n; y = n; z = n;   alert('x = '+ x + "\ny = " + y + "\nz = " + z);   }```
• Nov 8, 2010, 15:39
Novice2010
Quote:

Originally Posted by Kalon

Code:

``` function get_xyz () { var x = n; y = n; z = n;   alert('x = '+ x + "\ny = " + y + "\nz = " + z);   }```

I got the same thing as I did when I ran individual variables, how ever I am confused as to when I get undefined when I run the function.

Is it because the variables are outside the function?

Novice
• Nov 8, 2010, 15:44
Kalon
it would help to diagnose what the problem is if you posted your entire html + javascript file so we can see exactly what is happening and where you are running the document.write()
• Nov 8, 2010, 15:49
Novice2010
Quote:

Originally Posted by Kalon
it would help to diagnose what the problem is if you posted your entire html + javascript file so we can see exactly what is happening and where you are running the document.write()

Actually this is a practice problem.

Question: What is the value of x, y, and z if the code is executed?

I use the alert this time in stead of the document write.

Here is the entire code:
Code:

```var n=5; var x=n + n; var y=n + n; function get_xyz () { var x = n; y = n; z = n; } get_xyz(); alert(get_xyz ());```
Thanks as always!

N
• Nov 8, 2010, 16:13
Kalon
your function doesn't actually output anything. It just assigns values to variables and nothing else.

This works for me.

Code:

``` <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script  type="text/javascript"> var n=5; var x=n + n; var y=n + n; function get_xyz () { var x = n; y = n; z = n;   alert('x = '+ x + "\ny = " + y + "\nz = " + z); }   get_xyz();   </script> </head> <body> </body> </html>```
• Nov 8, 2010, 16:25
Novice2010
Quote:

Originally Posted by Kalon
your function doesn't actually output anything. It just assigns values to variables and nothing else.

This works for me.

Code:

``` <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script  type="text/javascript"> var n=5; var x=n + n; var y=n + n; function get_xyz () { var x = n; y = n; z = n;   alert('x = '+ x + "\ny = " + y + "\nz = " + z); }   get_xyz();   </script> </head> <body> </body> </html>```

Hey Kalon,

The correct answer was 10, 5, 5.

Personally, I think these questions stupid but what the heck.

Thanks my friend, as always, very helpful and patient.

Novice
• Nov 8, 2010, 16:28
Kalon
Quote:

Originally Posted by Novice2010
Hey Kalon,

The correct answer was 10, 5, 5.

but your function is resetting x,y and z to all equal n (which equals 5) with the lines

Code:

``` function get_xyz () {   var x = n;         y = n;         z = n; }```
and so the output will be 5,5,5 with your current code.
• Nov 8, 2010, 19:01
AussieJohn
It's also worth noting that "x" inside of the function is instantiated using "var", so it's a local variable to that function, which is the point of the exercise - to demonstrate scoping in JavaScript :)

If you would use an alert outside of the function it would use the "x" that was defined at the start before the function, but after the function is called, x and y will have their values re-assigned.

Consider:
Code:

```<script  type="text/javascript"> var n=5; var x=n + n; var y=n + n; function get_xyz () { var x = n; y = n; z = n;   alert("Inside function:\nx = "+ x + "\ny = " + y + "\nz = " + z);   } alert("1st Outside function:\nx = "+ x + "\ny = " + y + "\nz = "); // z is not defined yet get_xyz(); alert("2nd Outside function:\nx = "+ x + "\ny = " + y + "\nz = " + z); </script>```
Code:

```1st Outside function: x = 10 y = 10 z =```
The second will be:
Code:

```Inside function: x = 5 y = 5 z = 5```
And finally:
Code:

```2nd Outside function: x = 10 y = 5 z = 5```
• Nov 8, 2010, 19:19
Kalon
yep, it can get messy when you use variable names previously assigned to global variables as local variables in a function.

imho you should avoid using the same variable name as both local and global variables.

I'm not sure if that was the point of the exercise or if it was just an accidental error.

I think a clearer way of demonstrating scope would be something like:

Code:

``` <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script  type="text/javascript">   var x=5;  //global variable   alert('x outside function = '+ x);   function scope_demo() {     var y = 10; //local variable       alert('x inside function = '+ x +"\ny inside function = "+y);   } scope_demo();   alert("\ny outside function = "+ y);  //will generate an error saying y is undefined   </script> </head> <body> </body> </html>```
• Nov 8, 2010, 21:28
Novice2010
Quote:

Originally Posted by Kalon
yep, it can get messy when you use variable names previously assigned to global variables as local variables in a function.

imho you should avoid using the same variable name as both local and global variables.

I'm not sure if that was the point of the exercise or if it was just an accidental error.

I think a clearer way of demonstrating scope would be something like:

Code:

``` <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script  type="text/javascript">   var x=5;  //global variable   alert('x outside function = '+ x);   function scope_demo() {     var y = 10; //local variable       alert('x inside function = '+ x +"\ny inside function = "+y);   } scope_demo();   alert("\ny outside function = "+ y);  //will generate an error saying y is undefined   </script> </head> <body> </body> </html>```

Yes, I agree. My problem is I was just confused as to why the variable were outside of the function and then in the function.

Thanks again, I'll definitely be back very shortly. :)

Novice