How to set result of function as global variable.
Posted
by George
on Stack Overflow
See other posts from Stack Overflow
or by George
Published on 2010-03-25T15:09:26Z
Indexed on
2010/03/25
15:13 UTC
Read the original article
Hit count: 202
JavaScript
|global-variables
I'm sure this is really simple and I'm drawing a giant blank, but how do you set the result of a function as a global variable?
Example, I want to set the first "color" in array "colors" as global variable "color" (I know the example doesn't make much practical sense, but it's just to illustrate my question):
var color = "";
function selectColor () {
var colors = ["blue","red","green","yellow"];
var color = colors[0];
return color;
}
window.onload = function () {
selectColor ();
alert(color);
}
© Stack Overflow or respective owner