Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to define global variable in JavaScript ?

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 117
    Comment on it

    Hi friends, As we know that if we declared a variable outside the function of javascript, then it is said to be global variable. But this is not a correct way to declare global variable.

    Correct way to declare global variable in JavaScript is-

    when a global variable is set, it's added to the window object & use the syntax like this:
    var window.GlobalVar = "Hello";

    For Example

    var window.GlobalVar = "Hello";

    function Hello()
    {
        alert(window.GlobalVar); 
        //OR
        alert(GlobalVar); 
    }
    

    By doing in this way, JavaScript will be more robust and reliable.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: