As the others have said, you can use var at global scope (outside of all functions and modules) to declare a global variable:

<script>
var yourGlobalVariable;
function foo() {
    // ...
}
</script>

(Note that that's only true at global scope. If that code were in a module — <script type="module">...</script> — it wouldn't be at global scope, so that wouldn't create a global.)

Alternatively:

In modern environments, you can assign to a property on the object that globalThis refers to (globalThis was added in ES2020):

<script>
function foo() {
    globalThis.yourGlobalVariable = ...;
}
</script>

On browsers, you can do the same thing with the global called window:

<script>
function foo() {
    window.yourGlobalVariable = ...;
}
</script>

...because in browsers, all global variables global variables declared with var are properties of the window object. (The new let, const, and class statements [added in ES2015] at global scope create globals that aren't properties of the global object; a new concept in ES2015.)

(There's also the horror of implicit globals, but don't do it on purpose and do your best to avoid doing it by accident, perhaps by using ES5's "use strict".)

All that said: I'd avoid global variables if you possibly can (and you almost certainly can). As I mentioned, they end up being properties of window, and window is already plenty crowded enough what with all elements with an id (and many with just a name) being dumped in it (and regardless that upcoming specification, IE dumps just about anything with a name on there).

Instead, in modern environments, use modules:

<script type="module">
let yourVariable = 42;
// ...
</script>

The top level code in a module is at module scope, not global scope, so that creates a variable that all of the code in that module can see, but that isn't global.

In obsolete environments without module support, wrap your code in a scoping function and use variables local to that scoping function, and make your other functions closures within it:

<script>
(function() { // Begin scoping function
    var yourGlobalVariable; // Global to your code, invisible outside the scoping function
    function foo() {
        // ...
    }
})();         // End scoping function
</script>
Answer from T.J. Crowder on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_scope.asp
JavaScript Scope
All scripts and functions in the same web page can access a variable with global scope. Each JavaScript function have their own scope.
Top answer
1 of 16
947

As the others have said, you can use var at global scope (outside of all functions and modules) to declare a global variable:

<script>
var yourGlobalVariable;
function foo() {
    // ...
}
</script>

(Note that that's only true at global scope. If that code were in a module — <script type="module">...</script> — it wouldn't be at global scope, so that wouldn't create a global.)

Alternatively:

In modern environments, you can assign to a property on the object that globalThis refers to (globalThis was added in ES2020):

<script>
function foo() {
    globalThis.yourGlobalVariable = ...;
}
</script>

On browsers, you can do the same thing with the global called window:

<script>
function foo() {
    window.yourGlobalVariable = ...;
}
</script>

...because in browsers, all global variables global variables declared with var are properties of the window object. (The new let, const, and class statements [added in ES2015] at global scope create globals that aren't properties of the global object; a new concept in ES2015.)

(There's also the horror of implicit globals, but don't do it on purpose and do your best to avoid doing it by accident, perhaps by using ES5's "use strict".)

All that said: I'd avoid global variables if you possibly can (and you almost certainly can). As I mentioned, they end up being properties of window, and window is already plenty crowded enough what with all elements with an id (and many with just a name) being dumped in it (and regardless that upcoming specification, IE dumps just about anything with a name on there).

Instead, in modern environments, use modules:

<script type="module">
let yourVariable = 42;
// ...
</script>

The top level code in a module is at module scope, not global scope, so that creates a variable that all of the code in that module can see, but that isn't global.

In obsolete environments without module support, wrap your code in a scoping function and use variables local to that scoping function, and make your other functions closures within it:

<script>
(function() { // Begin scoping function
    var yourGlobalVariable; // Global to your code, invisible outside the scoping function
    function foo() {
        // ...
    }
})();         // End scoping function
</script>
2 of 16
36

Just declare

var trialImage;

outside. Then

function makeObj(address) {
    trialImage = [address, 50, 50];
    ...
    ...
}
Discussions

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
How do JavaScript’s global variables really work?
TLDR; Everyone lied to you when they said let and const don't create global variables. They absolutely do .. it's just that they don't create window. global variables. More on reddit.com
🌐 r/javascript
47
137
July 7, 2019
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › var
var - JavaScript | MDN - Mozilla
The var statement declares function-scoped or globally-scoped variables, optionally initializing each to a value. var x = 1; if (x === 1) { var x = 2; console.log(x); // Expected output: 2 } console.log(x); // Expected output: 2 ... var name1; var name1 = value1; var name1 = value1, name2 = ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Global_variable
Global variable - Glossary - MDN Web Docs
A global variable is a variable that is declared in the global scope in other words, a variable that is visible from all other scopes. In JavaScript it is a property of the global object.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › global-variable
Global Variables in JavaScript - Mastering JS
February 2, 2021 - In JavaScript, any variable defined outside any function or block is part of the global scope and is a global variable that any function can access.
🌐
Strapi
strapi.io › blog › global-variable-in-javascript
What Is a Global Variable in JavaScript?
November 25, 2025 - The variables share a name but not a memory location across the network boundary, so client-side mutations never affect server state. For values that rarely change, like API base URLs or feature flags read during initialization, this tradeoff often makes sense. For dynamic state that updates frequently, the false sense of synchronization makes globals a poor choice—one environment's changes never propagate to the other. JavaScript scope defines where variables are declared, accessible, and modifiable within your code.
Find elsewhere
🌐
Medium
habtesoft.medium.com › global-variables-in-javascript-understanding-and-best-practices-dbd7dd6f1067
Global Variables in JavaScript: Understanding and Best Practices | by habtesoft | Medium
November 13, 2024 - In JavaScript, global variables are variables that are accessible from any part of your code, regardless of where they are declared. However, working with global variables can lead to unintended consequences, especially when the scope and lifetimes ...
🌐
Tabnine
tabnine.com › home › global variable in javascript
Global Variable in JavaScript - Tabnine
July 25, 2024 - A Global variable is a variable that is declared in the global scope, making it a property of the global object. Global variables are accessible from all other (local) scopes. The global scope is a scope that contains every variable declared ...
🌐
Reddit
reddit.com › r/javascript › how do javascript’s global variables really work?
r/javascript on Reddit: How do JavaScript’s global variables really work?
July 7, 2019 - Unlike var which defines a variable globally, suggesting only var defines global variables and let would not. And since the only concept of global that ever existed in JavaScript before this was that the global object (window) is global, then it's easy to see how this could be surprising.
🌐
Javatpoint
javatpoint.com › javascript-global-variable
JavaScript global variable - javatpoint
JavaScript Variable JavaScript variable JavaScript Local variable JavaScript Global variable A JavaScript variable is simply a name of storage location. The actual value of a variable can be changed at any time.
🌐
web.dev
web.dev › articles › global and local variable scope
Global and local variable scope | Articles | web.dev
April 14, 2022 - When a variable is accessed within its scope, JavaScript returns its assigned value or otherwise produces an error. ... Use the var, const, or let keywords to declare local or global-scope variables.
🌐
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 - We’ll explain how to use globalThis to access the global object later in this article — but first, it’s important to understand why it was necessary. ... scopes (also called contexts) determine where a variable or function that you have declared is accessible. Each scope has access to the functions and variables declared in its parent scopes. There is a precedence to how JavaScript resolves variables that appear inside scopes: if variables of the same name have been declared in multiple scopes, JavaScript uses the first one it finds, starting at the current scope, and working up toward the global scope.
🌐
Snook
snook.ca › archives › javascript › global_variable
Global Variables in JavaScript - Snook.ca
Global variables are actually the default in JavaScript. The keyword var is actually what causes the variable you are assigning to be placed in the local scope.
🌐
freeCodeCamp
freecodecamp.org › news › global-variables-in-javascript-explained
Global Variables in JavaScript Explained
January 26, 2020 - Global variables are declared outside of a function for accessibility throughout the program, while local variables are stored within a function using var for use only within that function’s scope.
🌐
Exploring JS
exploringjs.com › deep-js › ch_global-scope.html
A detailed look at global variables • Deep JavaScript
In JavaScript, we are only in global scope at the top levels of scripts. In contrast, each module has its own scope that is a subscope of the script scope. If we ignore the relatively complicated rules for how variable bindings are added to the global environment, then global scope and module scopes work as if they were nested code blocks:
🌐
Medium
medium.com › @jambiebs › javascript-global-variable-fec37d62f44
JavaScript Global Variable. javascript global variable example | by Md Jamal | Medium
June 29, 2023 - When you declare a variable outside of any specific function or block, it becomes a global variable. This means that it can be accessed and modified from anywhere within the JavaScript code, including other functions, event handlers, or even ...
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_global_variables.htm
JavaScript - Global Variables
The global variables in JavaScript are the variables that are defined outside of the function or any particular block. They are accessible from anywhere in JavaScript code. All scripts and functions can access the global variables.
🌐
Flexiple
flexiple.com › javascript › global-variables
Global Variables In JavaScript - Flexiple
April 30, 2024 - Global Variables in JavaScript refers to variables that are accessible from any part of a JavaScript program. Developers declare global variables outside any function or simply omit the use of 'var', 'let', or 'const' inside a function.