Skip to content Skip to sidebar Skip to footer

How To Access Variable From Other Function In Javascript

I have one variable in function, I want to access it from other function. I cannot define this variable out of function. I set the example code for review. http://jsfiddle.net/VC6W

Solution 1:

I think this is what you are looking for.

functionone() {
var zero = 500;
two(zero);
}

functiontwo(a) {
alert(a);
}

Solution 2:

You can make your variable underneath the window global variable, if this is in a browser. So like this:

functionone() {
  window.zero = 500;
}

functiontwo() {
  alert(window.zero)
}

Solution 3:

Try like this

function one(){
    var zero= 500;
    return zero;
}

function two(){
    var alt = one();
    alert(alt);
}

Solution 4:

You can declare that variable globally in Javascript and then use/modify/access as needed

function one()
{
   myVar = 0;
   alert(myVar);
   two();
}

function two()
{
  alert(myVar);
}

Solution 5:

Try this :

function one(){
var zero = 500;
return zero;
}
function two(){   
alert(one());
}
two();

Or define any other variable globally and assign it the value of the 'zero' :

var zero_2;
function one(){
var zero = 500;
var zero_2 = zero;
}
function two(){   
alert(zero_2);
}
two();

Post a Comment for "How To Access Variable From Other Function In Javascript"