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
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];
    ...
    ...
}
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_scope.asp
JavaScript Scope
Before ES6, JavaScript variables could only have Global Scope or Function Scope. ES6 introduced two important new JavaScript keywords: let and const. These two keywords provide Block Scope in JavaScript. Variables declared with let and const inside a code block are "block-scoped," meaning they are only accessible within that block.
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
Global Scope and Functions
Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.โ€ " Variables which are declared without the let or const keywords are automatically created in the global scope. This can create unintended consequences ... More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
1
0
July 17, 2023
How can I declare global variables in React components?

What do you need from this element? Ideally, you'd pass that information down as props or use something like Redux and the react-redux package to help manage state.

More on reddit.com
๐ŸŒ r/learnjavascript
7
1
February 27, 2014
Global Variables
You can just export an object from any module. export const global = { foo: "and I call it a day } import { global } from "./global.ts"; global.foo = "Modified by a different file." Sure you might need to add an import statement everywhere, but then you'll be able to see everywhere it's being used as well More on reddit.com
๐ŸŒ r/typescript
3
2
February 11, 2024
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-declare-global-variables-in-javascript
How to declare Global Variables in JavaScript ? - GeeksforGeeks
July 23, 2025 - Note: If you assign a value to a variable and forgot to declare that variable, it will automatically be considered a global variable. In JavaScript, you can declare global variables by simply declaring them outside of any function or block scope.
๐ŸŒ
Quora
quora.com โ€บ In-JavaScript-is-there-a-way-from-within-a-function-to-declare-a-variable-to-the-global-scope
In JavaScript, is there a way from within a function to declare a variable to the global scope? - Quora
Answer (1 of 3): If you must declare a global variable, then do it OUTSIDE the function for goodnessโ€™ sake! Under normal circumstances, global variables should only be used for data that never changes once itโ€™s initialized.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Statements โ€บ var
var - JavaScript - MDN Web Docs - Mozilla
Be careful of the var x = y = 1 syntax โ€” y is not actually declared as a variable, so y = 1 is an unqualified identifier assignment, which creates a global variable in non-strict mode. ... var x = 0; function f() { var x = y = 1; // Declares x locally; declares y globally.
๐ŸŒ
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
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ understanding global variables in javascript
Understanding Global Variables in JavaScript | Hostman
December 26, 2025 - This example also shows that you may change the value of the global variable outside the function. ... The let and const keywords can also be used in declaring global variables in JS. ... Note: Variables declared using let can have their values changed, whereas constants declared with const cannot be reassigned. ... It's important to pay attention to best practices while using global variables in JavaScript in order to reduce potential problems and encourage maintainability.
Price ย  $
Call ย  +1 844 286 2130
Address ย  1999 Harrison St 1800 9079, 94612, Oakland
Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
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 - let globalVar = 'I am a global variable'; function showGlobal() { console.log(globalVar); // Accessible inside functions } showGlobal(); // Output: "I am a global variable" console.log(globalVar); // Output: "I am a global variable" In the example above, globalVar is declared in the global scope, meaning it can be accessed anywhere in the program. ... Passionate JavaScript developer with a focus on backend technologies.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is global variable javascript?
What is Global Variable JavaScript? - Scaler Topics
May 7, 2024 - As result, when add function get ... a global variable in javascript we have to declare a var, const or let variable outside of any block or function, inside the global scope....
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ global-variables-in-javascript-explained
Global Variables in JavaScript Explained
January 26, 2020 - var x = 5; // global function someThing(y) { var z = x + y; console.log(z); } function someThing(y) { x = 5; // still global! var z = x + y; console.log(z); } function someThing(y) { var x = 5; // local var z = x + y; console.log(z); } A global ...
๐ŸŒ
Quora
quora.com โ€บ Can-you-declare-a-global-variable-in-function-JavaScript
Can you declare a global variable in function JavaScript? - Quora
Answer: You technically can. But you shouldnโ€™t. If you need to variable to be global, it means it can be use anywhere at any time. If you declare it inside a function, it wonโ€™t be there until the function is called.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ javascript global variable
How to Declare Global Variables in JavaScript | Delft Stack
March 11, 2025 - In this example, globalVar is declared ... it successfully logs the value of globalVar. The var keyword is one of the traditional ways to declare variables in JavaScript....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ What-is-the-correct-way-to-define-global-variables-in-JavaScript
What is the correct way to define global variables in ...
June 13, 2020 - Even if we have defined the variable in the function, it becomes global as we haven't used any keyword to define the function. The output shows that variable 'a' can be accessible inside or outside the function. <html> <head> <title> JavaScript - Global variables </title> </head> <body> <p ...
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ javascript
Javascript Global Variables-How to create and access them in JS
September 12, 2022 - We can declare variables in javascript using โ€œvarโ€œ.Both these keywords allow the declaration of a variable. Now an obvious question is why we require two keywords to perform the same function. This is where the concept of scoping comes into the picture. Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript. The two types of scope are local and global:
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ javascript
Global Scope and Functions - JavaScript - The freeCodeCamp Forum
Variables which are defined outside ... everywhere in your JavaScript code.โ€ " Variables which are declared without the let or const keywords are automatically created in the global scope....
Published ย  July 17, 2023
๐ŸŒ
Medium
medium.com โ€บ @jambiebs โ€บ javascript-global-variable-fec37d62f44
JavaScript Global Variable. javascript global variable example | by Md Jamal | Medium
June 29, 2023 - In the example above, `myGlobalVariable` is declared with `var` outside of any function, making it a global variable. It can be accessed and used from both the `myFunction` function and the top-level scope. However, itโ€™s worth noting that in modern JavaScript, the `let` and `const` keywords are preferred over `var` due to their block scope.
๐ŸŒ
Snook
snook.ca โ€บ archives โ€บ javascript โ€บ global_variable
Global Variables in JavaScript - Snook.ca
If you want to declare a variable in the global scope from within any function, just omit the var keyword: (function () { x = 5; })(); alert((x * 10)); // alerts "50" This is really bad practice though, so refrain from doing so if at all possible.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ global-and-local-variables-in-javascript
Global and Local variables in JavaScript | GeeksforGeeks
Note: Use local variables whenever possible. Always use the var keyword to declare a new variable before the variable is referred to by other statements. ... JavaScript is a programming language used to create dynamic content for websites.
Published ย  August 20, 2024