Search code examples
javascriptscoping

javascript variable scoping


Why does javascript allow the creation of global variables in local code?
An example

function f() { x=10; }
function g() { print(x); }
f(x);
g(x);

Solution

  • Why does javascript allow the creation of global variables in local code?

    Because it isn't a perfect language.

    Use the var keyword to limit the scope of variables.