outside the function decalred variable is known as global variables
Interviews3 answers
What are global variables in javascript?
3 Answers
Bidyasagar MishraPosted Jul 17, 2019, 3:53 AM EDT
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);
Praveen DhatrikaPosted Jun 2, 2015, 8:05 PM EDT
Hi Mohan ,
var iAmGlobal = "some val"; //Global variable declaration
//Any place in other part of code
function doSomething()
{
//iAmGlobal = "changed value";
alert(iAmGlobal); //I am accessible here too !!
}
Amit TripathiPosted Jul 25, 2014, 6:24 AM EDT


