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. User Avatar
    billy_cool
    Permalink to comment#

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

  2. User Avatar
    Tim
    Permalink to comment#

    It’s bad practice to declare globals…

  3. User Avatar
    Pavithra
    Permalink to comment#

    I have problem in css global variable assigning, is it correct? but it not working how to fix this?
    ………………………
    @D:280px;
    @R:(@D/2); // radio
    -webkit-@R:(@D/2); // radio

    @B:(@R/8);
    -webkit-@B:(@R/8);

    @O: 0.6; // opacity
    -webkit-@O: 0.6;

    @A: 2s; // accelerate
    -webkit-@A: 2s;

    html,
    body{
    height: 100%;
    }

    body{
    overflow: hidden;
    }

    .hoja{
    color: rgb(220,220,226);
    position:absolute;
    top:50%; left:50%;

    -webkit- margin-left:-@R;
    margin-top:-@R;
    width:@D;
    height:@D;
    text-align:center;
    font-family: ‘Open Sans’, sans-serif;
    font-size:@R/4;
    line-height:@D;
    -webkit-font-smoothing: antialiased;
    }
    …………………………..

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-searchicon-staricon-tag