70

I am going through some code and at the beginning of the script we have var emailID = email. Later on, the code refers to emailID by going window.emailID. I am wondering what are the rules that allow you to refer to a variable by going window.variableName?

I cannot post my script online as it is directly related to my work and would violate my contract.

Jon
  • 2,249
  • 6
  • 26
  • 30
  • 14
    Note: For future reference, it should be safe to post a small snippet of code that's similar to the code you're working with, something that illustrates what you're having trouble with. – Matt Coughlin Jun 22 '12 at 01:29

6 Answers6

120

window.variableName means that the variable is being declared at the global scope. This means any JS code will have access to this variable. Using window. is not necessary but is frequently used as a convention to denote that a variable is global.

Globals are generally to be avoided. You should define variables within the scope of functions.

marteljn
  • 6,446
  • 3
  • 30
  • 43
  • 1
    Why do people use this for creating a global variable inside a function when the can simply create a variable outside the function and then modify the variable inside the function? – Shayan Nov 08 '19 at 13:38
  • @Shaya if, for some reason, you do not have access to that function or its return value in the context you require it (in some rare usages of a delegate, for example) – Peter David Carter Nov 05 '21 at 10:48
29

Global variables in JavaScript are attached to the "global object", which in a browser environment is aliased to window object - this is why you can refer to a global variable either as variableName or window.variableName.

It's also worth mentioning that using global variables in JavaScript is not considered good coding practice.

Here's a good and very detailed explanation.

Sergey
  • 1,608
  • 1
  • 27
  • 40
Sergey
  • 11,892
  • 2
  • 41
  • 52
18

window.myVar or window["myVar"] is an explicit way to refer to a global variable.

A variable is a global variable if it's declared outside of a function (with or without "var"), or if it's declared inside a function without using "var", or if it's declared as window.myVar or window["myVar"].

A variable is declared by either assigning a value to it, or by using the keyword var.

One case where it's useful to refer to a global variable as window.myVar is if you're inside a function that has a local variable called myvar. In that case, myVar refers to the local variable, while window.myVar refers to the global variable.

Matt Coughlin
  • 18,666
  • 3
  • 46
  • 59
14

Global Variables in JavaScript

var no =10;
function getNo()
   alert(no); // 10
}
getNo();

When a global variable is set, it's added to the window object!

var no =10;
function getNo()
   alert(window.no); // 10
}
getNo();

We can direct set window variable.

function setNo(){
  window.no=100;
}
function getNo()
   alert(window.no); // 100
}
setNo();
getNo();
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
2

For pure theoretical explanations (as I encountered this "issue" myself) and easy to digest information you can look at the problem as this:

var myName = "Bob" equals to - globalObject(Window) = { myName: "Bob" }

so when you declare a variable, that variable name is passed to the window object as a property and it's value as a property value. That's why you can call the variable as an object method of the window object, basically.

Eugen
  • 145
  • 2
  • 2
0

It is used to define global variable in JavaScript.

globalVar = "Hello World";
function function1(){
    alert(window.globalVar);
} 
function1();

This will print "Hello World" in the popup.

    function function1(){ 
        globalVar = "Hello World";
        alert(window.globalVar);
    }function function2(){
        alert(window.globalVar);
    } 
    function1(); 
    function2();

This will create two popups with value "Hello World", one from function1() and another from function2().

So, by using window we can call a variable from any scope in javascript.

Praveen Kishor
  • 2,413
  • 1
  • 23
  • 28