The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.
I'd like to know how you can create a variable inside a function and then use it inside a different function. for example...
function getName()
{
var name=prompt("your name","")
}
function alertNamed()
{
alert(name)
}
Very simple example, and I know it doesn't work. I know it's because the name variable is local, but I know you can declare a variable and then use it inside other functions. It has something to do with return variablename or something similar...
I know it's a really basic quesiton but I've scoured the web and can't find an answer anywhere.
Help?
Cheers :-)
I was hoping the 'global' bit would make the variable available to other functions, rather than just passing it.
I would be something like this, I think.
function alertNamed('name') {
alert(name);
}
but I'm not sure how to get the variable out of the first function, unless you call alertNamed(name) from inside the function .... but I was under the impression you don't want that???
No idea how to do it in JS though :-)
go on MSN and I'll annoy you about it.
What I need to do is create a variable inside a function and then pass it to another function. I'm sure it involves putting the variable name in the function name like: function rar(a_variable)
{
some stuff to decide the variable value then...
return a_variable
}
function use_the_variable_I_created(a_variable)
I'm sure it's something along those lines. I know I'm not maiing myself crystal clear though...
Lana - Making it global would just compromise what I need it for as it needs to be made on the call of a function.
function getName() {
global var name = prompt("your name","")
}
Are they going to be within the same bit of code?
why not declare the variable before the methods and see if that works.
I'd like to know how you can create a variable inside a function and then use it inside a different function. for example...
function getName()
{
var name=prompt("your name","")
}
function alertNamed()
{
alert(name)
}
Very simple example, and I know it doesn't work. I know it's because the name variable is local, but I know you can declare a variable and then use it inside other functions. It has something to do with return variablename or something similar...
I know it's a really basic quesiton but I've scoured the web and can't find an answer anywhere.
Help?
Cheers :-)