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:

document.write(get_xyx());

I get undefined

Here is the 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

if you add the red alert() you will see the result

 
function get_xyz () {
var x = n;
y = n;
z = n;
 
[COLOR=red]alert('x = '+ x + "\
y = " + y + "\
z = " + z);[/COLOR]
 
}
 

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

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:

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

your function doesn’t actually output anything. It just assigns values to variables and nothing else.

This works for me.

 
<!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;
 
[COLOR=red]alert('x = '+ x + "\
y = " + y + "\
z = " + z);
[/COLOR] 
} 
 
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

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

 
function get_xyz () {
   var x = n;
        y = n;
        z = n;
}


and so the output will be 5,5,5 with your current code.

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 :slight_smile:

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:


<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:\
x = "+ x + "\
y = " + y + "\
z = " + z);
 
} 
alert("1st Outside function:\
x = "+ x + "\
y = " + y + "\
z = "); // z is not defined yet
get_xyz();
alert("2nd Outside function:\
x = "+ x + "\
y = " + y + "\
z = " + z);

</script>

The first alert will be:


1st Outside function:
x = 10
y = 10
z =

The second will be:


Inside function:
x = 5
y = 5
z = 5

And finally:


2nd Outside function:
x = 10
y = 5 
z = 5

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:

 
<!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 +"\
y inside function = "+y);
 
} 
scope_demo();
 
alert("\
y 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. :slight_smile:

Novice