View Full Version : Local Variables

08-12-2004, 01:30 PM
It seems to me that if you have to functions, both with a local variable 'n', and the first function calls the second function and the second function increases the value of n, say in a while loop, the value of n will also have been increased in the first function when it continues.

I came across this problem while translating some simple code from Python to js.

Here is the js code. Change 'i' to 'x' in function printMultiples and it works, otherwise the value of i in printTables has been increased above the 'high' parameter when printMultiples is done and printTables continues


function printMultiples(n,high)
i = 1
document.write(n*i +"&nbsp;&nbsp;&nbsp;&nbsp; ")
i++ // also increases i in printTables
document.write("<br />")

function printTables(high)
i = 1
while (i < high)
alert(i + " printTables")



I would have understood this if there was a global variable i...?

Willy Duitt
08-12-2004, 01:57 PM
There's not one local variable in your entire script....

Without using the keyword var for your variables within your function calls....
Your variables are global....


08-12-2004, 02:02 PM
Thanks! now that I think about it I remember reading that before