All JavaScript code executes in some environment, most commonly in a browser. The code that executes must execute in some "root" scope referred to as the global context or global scope (think of it as the main container). In your browser, this "root" scope is the window object (unique window object per tab, page, or iframe).

That is why when in the example a variable gets declared in the global scope var carName = "Volvo"; you can access this variable on the window object window.carName, because in the browser the 'window' object the global object.

When you execute JavaScript code using Node.js for example the global object is very aptly named global and in that environment if you declare var carName = "Volvo"; you can also access the variable using global.carName (this is only true on the Node.js REPL; var declarations in files do not attach to the global object).

To elaborate:

var myObject = { };
myObject.myVariable = 1;
console.log(myObject.myVariable); // Logs 1

myVariable is created on myObject, and this is done explicitly.

var myVariable = 1; // Behind the scenes this declaration is doing window.myVariable = 1;
console.log(window.myVariable); // Logs 1

myVariable is implicitly created on the window object which in the context of a browser is the global object.

For maybe a better explanation, I strongly recommend this book series You Don't Know JS Yet (book series) - 2nd Edition, specifically for this question You Don't Know JS Yet - Scope & Closures - 2nd Edition

Answer from Heinrich Henning on Stack Overflow
Top answer
1 of 1
15

All JavaScript code executes in some environment, most commonly in a browser. The code that executes must execute in some "root" scope referred to as the global context or global scope (think of it as the main container). In your browser, this "root" scope is the window object (unique window object per tab, page, or iframe).

That is why when in the example a variable gets declared in the global scope var carName = "Volvo"; you can access this variable on the window object window.carName, because in the browser the 'window' object the global object.

When you execute JavaScript code using Node.js for example the global object is very aptly named global and in that environment if you declare var carName = "Volvo"; you can also access the variable using global.carName (this is only true on the Node.js REPL; var declarations in files do not attach to the global object).

To elaborate:

var myObject = { };
myObject.myVariable = 1;
console.log(myObject.myVariable); // Logs 1

myVariable is created on myObject, and this is done explicitly.

var myVariable = 1; // Behind the scenes this declaration is doing window.myVariable = 1;
console.log(window.myVariable); // Logs 1

myVariable is implicitly created on the window object which in the context of a browser is the global object.

For maybe a better explanation, I strongly recommend this book series You Don't Know JS Yet (book series) - 2nd Edition, specifically for this question You Don't Know JS Yet - Scope & Closures - 2nd Edition

🌐
W3Schools
w3schools.com β€Ί js β€Ί js_scope.asp
JavaScript Scope
In "Strict Mode", undeclared variables are not automatically global. With JavaScript, the global scope is the JavaScript environment. In HTML, the global scope is the window object.
Discussions

Difference between variable declaration syntaxes in Javascript (including global variables)? - Stack Overflow
AFAIK var a = 0; does not work in IE when accessing the variable via another external js file which is declared in another js file ... I donot know about window.a but the other 2 ways are the same in global scope. ... @Raynos, I experience it on my own website. IE6 to be specific. I could not get my var enum to appear which is on an external js file and i am referencing it as an inline javascript ... More on stackoverflow.com
🌐 stackoverflow.com
Global Variable in Javascript
You can access a value from any JavaScript code running in a window by adding it as property to the "global context", usually window in the browser, though you can also use globalThis which will work anywhere. window.myName = 'Sue'; globalThis.myAge = 37; // elsewhere in another file... console.log(globalThis.myName); // Sue console.log(window.myAge); // 37 It should be noted that this sort of setup works fine for a down-and-dirty weekend project, but scales poorly as your project grows. For one, a bunch of important stuff is already on the global context, and you may accidentally erase or alter it. You can mitigate this issue somewhat by using a single object for all of your global variables. window.MyGlobalVariables = {}; window.MyGlobalVariables.myName = 'Sue'; Still, even with this more cautious approach, there are few good reasons to use global variables. For almost any use case, you will want to use JavaScript modules with import and export . // In names.js... export const myName = 'Sue'; // In some other file... import { myName } from './names.js'; More on reddit.com
🌐 r/learnjavascript
6
1
August 5, 2024
javascript - window.variableName - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
Is using Global Variables always bad?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
6
6
November 28, 2023
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Glossary β€Ί Global_object
Global object - Glossary - MDN - Mozilla
In JavaScript, the global object always holds a reference to itself: ... console.log(globalThis === globalThis.globalThis); // true (everywhere) console.log(window === window.window); // true (in a browser) console.log(self === self.self); // true (in a browser or a Web Worker) console.log(frames ...
🌐
SymfonyCasts
symfonycasts.com β€Ί screencast β€Ί javascript β€Ί window-global-vars
The window Object & Global Variables
What I mean is: if you set a key on the window object, like RepLogApp, this becomes a global variable. That means you can reference RepLogApp from anywhere else, and this is actually referencing window.RepLogApp.
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί API β€Ί Window β€Ί window
Window: window property - Web APIs | MDN - Mozilla
May 23, 2025 - For example, if we refer to "this.window.location.href", a JavaScript module could define a property called "window" inside of a class it defined (since no global "window" variable exists for it by default) which could be created after passing in a window object to the module class' constructor.
🌐
JavaScript.info
javascript.info β€Ί tutorial β€Ί the javascript language β€Ί advanced working with functions
Global object
July 1, 2022 - alert("Hello"); // is the same as window.alert("Hello"); In a browser, global functions and variables declared with var (not let/const!) become the property of the global object:
🌐
Javatpoint
javatpoint.com β€Ί javascript-global-variable
JavaScript global variable - javatpoint
JavaScript global variable tutorial for beginners and professionals with example, declaring javascript global variable within function, internals of global variable in javascript, event, validation, object loop, array, document, tutorial
Find elsewhere
🌐
Codedamn
codedamn.com β€Ί news β€Ί javascript
Javascript Global Variables-How to create and access them in JS
September 12, 2022 - There’re a few ways to do this. One way is to define it as a property of the window object. Then we can reference it as foo or window.foo anywhere in our code. window.foo = 1;Code language: JavaScript (javascript)
🌐
Flexiple
flexiple.com β€Ί javascript β€Ί global-variables
Global Variables In JavaScript - Flexiple
April 30, 2024 - Global variables are declared outside ... global object. In a web browser, the global object is window, so all global variables are properties of the window object....
🌐
EyeHunts
tutorial.eyehunts.com β€Ί home β€Ί window variable javascript | declare global variables inside a function
Window Variable JavaScript | Declare Global Variables inside a function
May 16, 2021 - A variable gets declared in the global scope window.carName = "Volvo"; can access this variable on the window object window.carName, because in the browser the β€˜window’ object the global object.
🌐
SitePoint
sitepoint.com β€Ί blog β€Ί javascript β€Ί understanding the javascript window object
Understanding the JavaScript Window Object β€” SitePoint
November 13, 2024 - The JavaScript Window Object represents ... for all JavaScript within a browser context. Any global variables created are properties of the window object and any functions are its methods....
🌐
2ality
2ality.com β€Ί 2013 β€Ί 09 β€Ί window.html
Tips for using window in JavaScript
Style checkers. When working with a style checking tool such as JSLint and JSHint, using window means that you don’t get an error when referring to a global variable that is not declared in the current file.
🌐
Contentful
contentful.com β€Ί blog β€Ί the-global-object-in-javascript
What is the global object in JavaScript? A practical guide for developers | Contentful
March 14, 2024 - In addition to these, specific ... global objects contain variables for the cache, the console, and window geometry, as well as functions for things like displaying alert pop-ups and working with base64....
Top answer
1 of 5
581

Yes, there are a couple of differences, though in practical terms they're not usually big ones (except for your #2 β€” a = 0; β€” which A)  I strongly recommend not doing, and B) is an error in strict mode).

There's a fourth way, and as of ES2015 (ES6) there's two more. I've added the fourth way at the end, but inserted the ES2015 ways after #1 (you'll see why), so we have:

Copyvar a = 0;                             // 1
let a = 0;                             // 1.1 (new with ES2015)
const a = 0;                           // 1.2 (new with ES2015)
a = 0;                                 // 2
window.a = 0; /*or*/ globalThis.a = 0; // 3
this.a = 0;                            // 4

Those statements explained

1. var a = 0;

This creates a global variable which is also a property of the global object, which we access as window on browsers (or via the globalThis global added in ES2020, or via this at global scope). Unlike some other properties, the property cannot be removed via delete.

In specification terms, it creates an identifier binding on the Object Environment Record for the global environment. That makes it a property of the global object because the global object is where identifier bindings for the global environment's Object Environment Record are held. This is why the property is non-deletable: It's not just a simple property, it's an identifier binding, and identifiers can't be removed.

The binding (variable) is defined before the first line of code runs (see "When var happens" below).

The property this creates is enumerable (except on the very obsolete IE8 and earlier).


1.1 let a = 0;

This creates a global variable which is not a property of the global object. This is a new thing as of ES2015.

In specification terms, it creates an identifier binding on the Declarative Environment Record for the global environment rather than the Object Environment Record. The global environment is unique in having a split Environment Record, one for all the old stuff that goes on the global object (the Object Environment Record) and another for all the new stuff (let, const, and the functions created by class) that don't go on the global object, but go in the global environment's Declarative Environment Record instead.

The binding is created before any step-by-step code in its enclosing block is executed (in this case, before any global code runs), but it's not accessible in any way until the step-by-step execution reaches the let statement. Once execution reaches the let statement, the variable is accessible. (See "When let and const happen" below.) The time between the binding being created (on entry to the scope) and becoming accessible (code execution reaching the let) is called the Temporal Dead Zone [TMZ]. While the binding is in that state, any attempt to read from it or write to it is a runtime error.

(The specification's terminology for whether the binding is accessible is whether it's "initialized," but don't confuse that use of "initialized" with having an initializer on the let statement [let a = 10; vs. just let a;]; they're unrelated. The variable defined by let a; is initialized with undefined once the let is reached.)


1.2 const a = 0;

Creates a global constant, which is not a property of the global object.

A const binding is exactly like a let binding (including the TMZ and such) except it has a flag saying its value cannot be changed. One implication of that is you must provide an initializer (the = value part) to provide the initial (and never-changing) value for the const.

Using const does three things for you:

  1. Makes it a runtime error if you try to assign to the constant (and most IDEs will flag it up for you more proactively than that).
  2. Documents its unchanging nature for other programmers.
  3. Lets the JavaScript engine optimize on the basis that the const's value won't change (without having to track whether it's written to later or not β€” e.g., doesn't have to check if it's effectively constant).

It's important to understand that the const's value never changing doesn't mean that an object the const refers to is immutable. It isn't. It just means that the value of the const can't be changed so it refers to a different object (or contains a primitive):

Copy// This is fine:
const x1 = {a: 1};
console.log(x1.a); // 1
x1.a = 2;
//^^^^^^βˆ’βˆ’βˆ’ No problem, just changing the object's state, not the value in the `const` (the object reference)
console.log(x1.a); // 2

// This is not:
const x2 = {a: 1};
console.log(x2.a); // 1
x2 = {a: 2};
// ^βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’βˆ’ Error here ("TypeError: Assignment to constant variable"),
// you can't change the value of a `const`
console.log(x2.a);
Run code snippetEdit code snippet Hide Results Copy to answer Expand


2 a = 0;

Don't do this. It's assigning to a completely undeclared identifier. In loose mode (the only mode before ES5), it creates a property on the global object implicitly. On my old blog, I call this The Horror of Implicit Globals. Thankfully, they fixed it with strict mode, added in ES5 and the default in new kinds of scopes (inside modules, inside class constructs, etc.). Strict mode makes assigning to an undeclared identifier the error it always should have been. It's one of several reasons to use strict mode.

Since it creates a normal property, you can delete it.

The property this creates is enumerable (except on the very obsolete IE8 and earlier).


3 window.a = 0; or globalThis.a = 0;

This creates a property on the global object explicitly, using the window global (on browsers) or the globalThis global that refers to the global object. As it's a normal property, you can delete it.

This property is enumerable (even on the very obsolete IE8 and earlier).


4 this.a = 0;

Exactly like #3, except we're referencing the global object through this instead of the globals window or globalThis. This works because this at global scope is the "global" this value. This is true even in strict mode. (Strict mode changes the this used when you call a function without supplying this, such as when you do fn(), but not what this is at global scope.) Note that it has to really be global scope. The top-level scope of modules is not global scope (it's module scope), and at module scope this is undefined.


Deleting properties

What do I mean by "deleting" or "removing" a? Exactly that: Removing the property (entirely) via the delete keyword:

Copywindow.a = 0;
console.log(`"a" in window? ${"a" in window}`); // "a" in window? true
delete window.a;
console.log(`"a" in window? ${"a" in window}`); // "a" in window? false
Run code snippetEdit code snippet Hide Results Copy to answer Expand

delete completely removes a property from an object. You can't do that with properties added to window indirectly via var, the delete is either silently ignored or throws an exception (depending on whether you're in strict mode).

(Minor note: The very obsolete IE8 and earlier, and the obsolete IE9-IE11 in their broken "compatibility" mode, wouldn't let you delete window properties even if you should have been allowed to.)


When var happens

Preface: var has no place in new code. Use let or const instead. But it's useful to understand var for the purposes of understanding old code you run across.

The variables defined via the var statement are created before any step-by-step code in the execution context is run, and so the variable (and its property on the global object) exists well before the var statement.

This can be confusing, so let's take a look. Here we have code trying to access a and b, followed by code in the middle creating them, and then code trying to access them again:

Copytry {
    console.log(a);   // undefined
    console.log(b);   // ReferenceError: b is not defined
} catch (e) {
    console.error(e);
}

var a = "ayy";
b = "bee";            // Don't do this, but I didn't want to use `let` or `const` in this example

try {
    console.log(a);   // "ayy"
    console.log(b);   // "bee"
} catch (e) {
    console.error(e);
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand

As you can see, the identifier a is defined (with the value undefined) before the first line runs, but the identifier b isn't, so trying to read its value is a ReferenceError. The statement var a = "ayy"; really deos two different things, at different times: On entry to the scope, it defines the identifier with the initial value undefined (the var a part), and later when it's reached in the execution of the code, it sets the value of a (the a = "ayy" part). Since a is defined before the first line of code runs, we can use it (and see its undefined value). This is known as "var hoisting" because the var a part is moved ("hoisted") to the top of the global scope or function scope where it appears, but the a = "ayy" part is left in its original location. (See Poor misunderstood var on my anemic old blog.)


When let and const happen

let and const are different from var in a couple of useful ways. The ways that are relevant to the question are A) that although the binding they define is created before any step-by-step code runs, it's not accessible until the let or const statement is reached; and B) as we've seen above, at global scope they don't create properties on the global object.

Re (A), while this using var runs:

Copyconsole.log(a); // undefined
var a = 0;
console.log(a); // 0
Run code snippetEdit code snippet Hide Results Copy to answer Expand

This using let throws an error:

Copyconsole.log(a); // ReferenceError: a is not defined
let a = 0;
console.log(a);
Run code snippetEdit code snippet
2 of 5
40

Keeping it simple :

Copya = 0

The code above gives a global scope variable

Copyvar a = 0;

This code will give a variable to be used in the current scope, and under it

Copywindow.a = 0;

This generally is same as the global variable.

🌐
Snook
snook.ca β€Ί archives β€Ί javascript β€Ί global_variable
Global Variables in JavaScript - Snook.ca
As a last resort, you can enclose the code that requires this global variable in a closure and declare it as local within that scope; in this way, the variable that used to be 'truly' global (a property of window) is now 'global' to the code within that closure.
🌐
Strapi
strapi.io β€Ί blog β€Ί global-variable-in-javascript
What Is a Global Variable in JavaScript?
November 25, 2025 - JavaScript implements three primary ... applications with multiple scripts and libraries. Variables in global scope become accessible from anywhere in your application. In browsers, this means the window object; in Node.js, ...
🌐
Tobyho
tobyho.com β€Ί 2013 β€Ί 03 β€Ί 13 β€Ί window-prop-vs-global-var
A Subtle Difference Between Window Properties and Global Variables
March 13, 2013 - then, our original code snippet would successfully use the existing array, because Javascript ignores the var statement in that case and works as if it was ... most browsers will treat _gaq equivalent to a global variable, and for those browsers, it would work exactly the same, except for the case of IE8 and below. For IE8, given that _gaq is a window property and not a global variable, and also given that the var _gaq = _gaq || []; GA snippet is pasted inside its own <script> tag, then the following applies.
🌐
Davidwells
davidwells.io β€Ί snippets β€Ί get-user-defined-window-variables
Get user defined window global variables with javascript
June 3, 2019 - function getWindowVars() { // create an iframe and append to body to load a clean window object const iframe = document.createElement('iframe') // hide iframe iframe.style.display = 'none' // add iframe to page document.body.appendChild(iframe) // get the current list of properties on window const currentWindow = Object.getOwnPropertyNames(window) // filter the list against the properties that exist in the clean window const results = currentWindow.filter(function(prop) { return !iframe.contentWindow.hasOwnProperty(prop); }) // Clean up iframe document.body.removeChild(iframe) return results } // usage: const userDefinedVars = getWindowVars() console.log(userDefinedVars) For example you might want to see if browser extensions are polluting the global window or what analytics tools a site has installed.