/    Sign up×
Community /Pin to ProfileBookmark

Accessing Variable Within Another Fucntion

How do I got about accessing a variable located within another function?

Any help would be greatly appreciated.

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayMar 17.2005 — You'll either have to make it a global variable (one defined outside of all functions), pass the value to the second function from the first, or store the value on the page (usually a hidden field) and read it from there.
Copy linkTweet thisAlerts:
@iceman2gauthorMar 17.2005 — How would I pass the value to the second function?
Copy linkTweet thisAlerts:
@scragarMar 17.2005 — var x = "hello";

function foo1(){

alert(x);

};

foo1();




function foo2(somevar){

alert(somevar);

};

foo2("hello");
Copy linkTweet thisAlerts:
@TheBearMayMar 17.2005 — [code=php]

function firstFunc(){
var valueForSecond = 20;
....
secFunc(valueForSecond);
...
}

function secFunc(passedVal){
...
alert(passedVal);
return;
}
[/code]
Copy linkTweet thisAlerts:
@iceman2gauthorMar 17.2005 — For future how do I define a global variable in js.
Copy linkTweet thisAlerts:
@TheBearMayMar 17.2005 — [code=php]
<script type="text/javascript">

var gVar;

function....
</script>[/code]
Copy linkTweet thisAlerts:
@iceman2gauthorMar 17.2005 — Now would this mean that if a variable were set within a fucntion I could make that variable a global one by doing var Variablename; outside the function.
Copy linkTweet thisAlerts:
@TheBearMayMar 17.2005 — I think you're saying the right thing:

[code=php]
<script type="text/javascript">

var gVar=0;

function funOne(){
alert(gVar);
gVar=20;
funTwo();
}

function funTwo(){
alert(gVar)
}

funOne();
</script>
[/code]
Copy linkTweet thisAlerts:
@7studMar 17.2005 — How do I got about accessing a variable located within another function?[/quote]
Variables declared within a function are called "local variables", i.e. they are local to the function, and when the function ends, the local variables are destroyed and cannot be accessed anymore.

Variables declared outside of any functions are called "global variables", and they can be accessed from inside a function by referrring to them by their name.

There are several methods for declaring global variables:
var x = 100; //global variable--it is declared outside any function

function A()
{
alert(x);
}

function B()
{
alert(x);
}

A();
B();


<i>
</i>function A()
{
window["x"] = 100; //creates a global variable
}

function B()
{
alert(x);
}

A(); //creates the global variable
B(); //accesses the global variable

not recommended:
<i>
</i>function A()
{
x = 100; //if you don't use 'var', it creates a global variable
}

function B()
{
alert(x);
}

A(); //creates the global variable
B(); //accesses the global variable
Copy linkTweet thisAlerts:
@iceman2gauthorMar 18.2005 — Ok here's my delima. I have two drop menus. When an option is selected from either menu it updates prices on a page. The problem is i'm not sure how to update the price to take into account the selection of both menu selection.

i.e.

Color:

Red +2

Green +3

Blue +4

Size:

sm

med +2

lrg +3

ex-lrg +4

Currently i'm only able to update the price of the page with the selection of one of the two drop menus.
Copy linkTweet thisAlerts:
@iceman2gauthorMar 18.2005 — Ok I think I may have this figured out. Just a quick question how do I check to make sure a variable is set.

In php it's isset() but unsure of the js equivalent.
Copy linkTweet thisAlerts:
@iceman2gauthorMar 19.2005 — Any one knwo if there's a function in js that's similiar to the isset function in php?
Copy linkTweet thisAlerts:
@7studMar 19.2005 — var x;

if(x) alert("x has a value");
else alert(x);

x = 100;

if(x) alert("x has a value");
else alert(x);

//alert(y); error!

In js, undefined evaluates to false. It's a little confusing, but the last line will give you a "not defined or undefined error", but the way it works is if the variable hasn't been declared, you get an error, but if the variable has been declared, but it hasn't been assigned a value, then it is equal to "undefined".
Copy linkTweet thisAlerts:
@iceman2gauthorMar 21.2005 — Thanks guys I figured it out.
×

Success!

Help @iceman2g spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...