2
Reply

What are global variables in javascript?

Mohan Gupta

Mohan Gupta

11y
804
0
Reply

    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);

    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 !! }