...

View Full Version : why the result is NaN?



runeveryday
09-29-2012, 02:33 PM
var a = 10;
sayHi();
function sayHi()
{
var a = a + 10;
alert(a);
return a;
}
alert(a);
alert(sayHi()+10);

why the above result is not 20 and 30? i feel the first is 20 , then 30.in sayHi() function, there is a line var a; i don't know why the a is undefined。 thank you

xelawho
09-29-2012, 02:37 PM
redeclaring "a" as a variable creates a new variable and wipes the value of the original. Remove the "var" in your function:


var a = 10;
sayHi();
function sayHi()
{
a = a + 10;
alert(a);
return a;
}
alert(a);
alert(sayHi()+10);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum