Grow your CSS skills. Land your dream job.

Last updated on:

Global Variables

Declare variable outside of the function...

var oneVariable;

function setVariable(){
    oneVariable = "Variable set from within a function!";
}

function getVariable(){
    alert(oneVariable); // Outputs "Variable set from within a function!"
}

Or... attach it to the window object

function setValue() {
    window.myValue = "test";
}

function getValue() {
    alert(window.myValue); // "test" (assuming setValue has run)
}

Comments

  1. billy_cool
    Permalink to comment#

    don’t even need to specify “var” when you are declaring a global.

  2. Permalink to comment#

    It’s bad practice to declare globals…

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".