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.

Answer from marteljn on Stack Overflow
🌐
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.
Discussions

The window variable and temporarily storage
When I declare my custom variable like window.myflag and assign to it something, like a string or a class etc… what is its scope? The current browser window or the current tab? I tried to access it from my option page but it was not possible. Is there some way to store temporary data or session ... More on discourse.mozilla.org
🌐 discourse.mozilla.org
0
0
April 9, 2022
javascript - Window vs Var to declare variable - Stack Overflow
Possible Duplicate: Difference between using var and not using var in JavaScript Should I use window.variable or var? I have seen two ways to declare a class in javascript. like window.ABC ... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Should I use window.variable or var? - Stack Overflow
I stand corrected; I should be ... in the JavaScript console ... The general answer to the question would be to use var. More specifically, always put your code in an Immediately Invoked Function Expression (IIFE): ... This keeps variables like foo and bar from polluting the global namespace. Then, when you explicitly want a variable to be on the global object (typically window) you can ... More on stackoverflow.com
🌐 stackoverflow.com
Accessing browser window variables
I'm trying to access a global variable that exists in a browser window (not in KM). For example, in Chrome's console, I can access the variable TWA, but doing that through the 'Execute JavaScript in Front Browser action it's always undefined. I also tried 'document.TWA' and 'window.TWA', but ... More on forum.keyboardmaestro.com
🌐 forum.keyboardmaestro.com
0
0
February 4, 2022
🌐
W3Schools
w3schools.com › js › js_window.asp
JavaScript Window - The Browser Object Model
Global variables are properties of the window object. Global functions are methods of the window object. Even the document object (of the HTML DOM) is a property of the window object: ... Two properties can be used to determine the size of the ...
🌐
SitePoint
sitepoint.com › blog › javascript › understanding the javascript window object
Understanding the JavaScript Window Object — SitePoint
November 13, 2024 - Every JavaScript environment has a global object. Any variables that are created in the global scope are actually properties of this object, and any functions are methods of it. In a browser environment the global object is the window object, ...
🌐
Mozilla Discourse
discourse.mozilla.org › add-ons › development
The window variable and temporarily storage - Development - Mozilla Discourse
April 9, 2022 - When I declare my custom variable like window.myflag and assign to it something, like a string or a class etc… what is its scope? The current browser window or the current tab? I tried to access it from my option page bu…
🌐
SymfonyCasts
symfonycasts.com › screencast › javascript › window-global-vars
The window Object & Global Variables
Here's the deal: when you're executing ... variable. In fact, it's even more important than that. This window variable holds all of the global variables....
Find elsewhere
🌐
2ality
2ality.com › 2013 › 09 › window.html
Tips for using window in JavaScript
But you can normally achieve the same result by treating a global variable as a variable and not as a property of window. This is the recommended way of doing it, because your code becomes slightly more flexible and because the global scope will become less used in the future. The idea for this blog post came from the answers of a tweet, where I asked why and how people use window. ... Dr. Axel Rauschmayer Homepage | Mastodon · Exploring JavaScript Book (free online) and exercises
🌐
Keyboard Maestro
forum.keyboardmaestro.com › questions & suggestions
Accessing browser window variables - Questions & Suggestions - Keyboard Maestro Discourse
February 4, 2022 - I'm trying to access a global variable that exists in a browser window (not in KM). For example, in Chrome's console, I can access the variable TWA, but doing that through the 'Execute JavaScript in Front Browser action it's always undefined. I also tried 'document.TWA' and 'window.TWA', but ...
🌐
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)
🌐
Medium
rahul319sinha.medium.com › understanding-the-window-object-global-execution-context-and-this-in-javascript-8e02549b195b
Understanding the window Object, Global Execution Context, and this in JavaScript | by Rahul Kumar | Medium
May 16, 2025 - The global execution context also defines the this keyword to point to the window object. Variables/functions defined with var and function declarations in the global space are added as properties to window.
Top answer
1 of 3
126

In JavaScript, any global variable is actually a property of the window object. Using one is equivalent to (and interchangeable with) using the other.

Using global variables is certainly "common," so the question is whether or not it's "proper." Generally, global variables are discouraged, because they can be accessed from ANY function and you risk having multiple functions trying to read from and write to the same variables. (This is true with any programming language in any environment, not just JavaScript.)


Solve this problem by creating a namespace unique to your application. The easiest approach is to create a global object with a unique name, with your variables as properties of that object:

window.MyLib = {}; // global Object container; don't use var
MyLib.value = 1;
MyLib.increment = function() { MyLib.value++; }
MyLib.show = function() { alert(MyLib.value); }

MyLib.value=6;
MyLib.increment();
MyLib.show(); // alerts 7

Another approach is to use .data() to attach variables to a relevant DOM element. This is not practical in all cases, but it's a good way to get variables that can be accessed globally without leaving them in the global namespace.

2 of 3
12

What is actually not mentioned here, and is probably the biggest deal breaker on why not to use window as global scope carrier is that it can be frozen (not writable) in some cases (and I'm talking from production based experience).

A good pattern is actually just create a global variable that will be used for all the common stuff in your application

var Application = {};
Application.state = { name: 'Application' }
.
.

What I found as the best pattern for me in javascript is to have a global state using Redux.

🌐
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)
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Grammar_and_types
Grammar and types - JavaScript - MDN - Mozilla
In web pages, the global object is window, so you can read and set global variables using the window.variable syntax. In all environments, the globalThis variable (which itself is a global variable) may be used to read and set global variables. This is to provide a consistent interface among various JavaScript runtimes.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Window
Window - Web APIs - MDN Web Docs - Mozilla
A global variable, window, representing the window in which the script is running, is exposed to JavaScript code.
Top answer
1 of 2
2

If this code is executed in a browser, window is available as a global object. However, if this code is executed in a node environment (server side), window will be undefined.

Here's one way to handle this if your code will execute both server-side (node environment) and client-side (browser environment):

if (typeof window !== 'undefined') {
  configs = {
    //firebase configuration
    apiKey: window._env_.API_KEY,
    projectId: window._env_.PROJECT_ID,
    messagingSenderId: window._env_.MESSAGING_SENDER_ID,
    backendURL: window._env_.BACKEND_URL
  }
} else {
  // handle server-side logic here
}

If there's no need for this to execute in the browser, it would be simplest to just use process.env instead of setting these variables on the window. If you do need these variables in both places (and they're coming from process.env), this might be another solution:

const env = typeof window === 'undefined' ? process.env : window._env_;

export const configs = {
  //firebase configuration
  apiKey: env.API_KEY,
  projectId: env.PROJECT_ID,
  messagingSenderId: env.MESSAGING_SENDER_ID,
  backendURL: env.BACKEND_URL
}
2 of 2
1

You should create a new js file, then write as below. File name is appsettings.js

window.appSettings = {
    apiKey: '${REACT_API_KEY}',
    projectId: '${REACT_PROJECT_ID}',
    messagingSenderId: '${REACT_SENDER_ID}',
    backendURL: '${REACT_BACKEND_URL}'
};

env files

REACT_API_KEY=value
REACT_PROJECT_ID=value
REACT_SENDER_ID=value
REACT_BACKEND_URL=value

Public/index.html

<script src="http://localhost/appsettings.js?v={version}"></script>
    <script>
        window.appSettings.apiKey= '%REACT_API_KEY%';
        window.appSettings.projectId= '%REACT_PROJECT_ID%';
        window.appSettings.messagingSenderId= '%REACT_SENDER_ID%';
        window.appSettings.backendURL= '%REACT_BACKEND_URL%';
    </script>

Where you want to use, write this code

window.appSettings.apiKey

Not need declare another place because it declare index.html

🌐
Medium
medium.com › @Chitturi_Teja › all-about-javascript-window-object-9dbcd24463ad
All about JavaScript Window object | by Teja Chitturi | Medium
October 16, 2024 - Each browser tab gets its own unique window object, ensuring that JavaScript running in one tab is isolated from another. When you declare a variable using var outside of any function, it becomes a property of the window object: