window.variableName

Javascript

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonView Question on Stackoverflow
Solution 1 - JavascriptmarteljnView Answer on Stackoverflow
Solution 2 - JavascriptSergeyView Answer on Stackoverflow
Solution 3 - JavascriptMatt CoughlinView Answer on Stackoverflow
Solution 4 - JavascriptNanhe KumarView Answer on Stackoverflow
Solution 5 - JavascriptEugenView Answer on Stackoverflow
Solution 6 - JavascriptPraveen KishorView Answer on Stackoverflow