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 variable
greeting = "JavaScript Global variable example";
}
GlobalEx();
// Variable greeting is available outside the function
alert(greeting);