2
Reply

What are global variables in javascript?

Mohan Gupta

Mohan Gupta

May 08, 2013
785
0

    Global variables are the variables declared outside a function. Global variables have global scope meaning all scripts and functions on the page can access them. The lifetime of a global variable starts with it's declaration and is deleted when the page is closed.Also, If you assign a value to a variable that has not been declared, it will automatically become a global variable, even if it is present inside a function.function GlobalEx() {// The variable greeting is not declared but a value is assigned.// So it will automatically become a global variablegreeting = "JavaScript Global variable example"; }GlobalEx();// Variable greeting is available outside the functionalert(greeting);

    Praveen Dhatrika
    June 03, 2015
    0

    Hi Mohan ,var iAmGlobal = "some val"; //Global variable declaration//Any place in other part of codefunction doSomething() {//iAmGlobal = "changed value";alert(iAmGlobal); //I am accessible here too !! }

    Amit Tripathi
    July 25, 2014
    0