Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.variableName

Tags:

javascript

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.

like image 330
Jon Avatar asked Jun 22 '12 01:06

Jon


People also ask

What are window variables?

Window Variable means that the variable is being declared at the global scope In JavaScript. Window Variable is used to declare JavaScript global variables inside the function. However, it's not recommended to create or declarer Global variables generally and it should be avoided.

How do you set a global variable in Windows?

Windows 10 and Windows 8Search and select System (Control Panel). Click on the Advanced system settings link and then click Environment Variables. Under the section System Variables, select the environment variable you want to edit, and click Edit. If the environment variable you want doesn't exist, click New.

What is the difference between window and global?

No difference. They both have the same effect (In the browser, where window is the global context1).

What is window [] in JS?

The window object is supported by all browsers. It represents the browser's window. All global JavaScript objects, functions, and variables automatically become members of the window object. Global variables are properties of the window object. Global functions are methods of the window object.


1 Answers

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.

like image 154
marteljn Avatar answered Sep 26 '22 03:09

marteljn