Jump to content

moving variable from function to function


seventheyejosh

Recommended Posts

hello all.

 

my question is this.

 

is there a way to create a variable in one function and have it availible in another?

 

like:

 

function one(){

 

    var one=1;

 

}

 

then have it useable in function 2?

 

function two(){

 

    alert('value of one is '+one);

 

}

 

thanks to all :)

 

You issue deal with variable scope. If you initialize a variable within a function, then that variable only has scope in that function. To make a variable have global scope, then declare it outside of the functions

 

//Declare the variable
var one;

function one()
{
     var one=1;
}

function two()
{
     alert('value of one is '+one);
}

two(); //Will alert "Value of one is 1"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.