Jon
Jon

Reputation: 2399

window.variableName

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.

Upvotes: 79

Views: 170677

Answers (6)

Sergey
Sergey

Reputation: 12417

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.

Upvotes: 29

Praveen Kishor
Praveen Kishor

Reputation: 2523

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.

Upvotes: 0

Eugen
Eugen

Reputation: 145

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.

Upvotes: 2

Nanhe Kumar
Nanhe Kumar

Reputation: 16307

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

Upvotes: 14

marteljn
marteljn

Reputation: 6516

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.

Upvotes: 124

Matt Coughlin
Matt Coughlin

Reputation: 18906

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.

Upvotes: 19

Related Questions