StaticVariable StaticVariable - 1 month ago 10
HTML Question

change global variable value using a local function in java script

I have written a very simple java script code. I want to change the value of global variable using a local function. I want that when I call the

value1()
function than the output should be "2". How can I perform this

var xhr=1;
alert(xhr);

function vari(){
xhr=2;
alert(xhr);
}

function value1(){
//here the value should be 2
alert(xhr);
}

Answer

Your code works fine! You have to invoke vari() though, but you know that, right? You cannot expect variables to change, if you do not call the functions that change the values!

Comments