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
March 5, 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 3, 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.
🌐
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.
🌐
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
For example javascript has var. Of course, noone NEEDS any. ... IBM FlashSystem from CDW works in hybrid, on-prem, and cloud setups with advanced security throughout. ... Web designer since the internets were invented · Author has 12.9K answers and 11.2M answer views · 3y · 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.
🌐
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
🌐
W3docs
w3docs.com › javascript
How to Define Global Variable in a JavaScript Function
Alternatively, assign the property to a window because, in JavaScript, you can define global variables inside a function using the window object.
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 ...
🌐
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   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
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....
🌐
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.
🌐
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.
🌐
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
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 ...
🌐
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....
🌐
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
🌐
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 ...
🌐
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.
🌐
Strapi
strapi.io › blog › global-variable-in-javascript
What Is a Global Variable in JavaScript?
November 25, 2025 - You'll learn what counts as a global variable across different environments, how scope layers—global, function, and block—determine variable access, and discover modern patterns like globalThis, modules, and namespacing that keep your code collision-free. Most importantly, you'll adopt practical strategies that prevent unexpected conflicts before they reach production. ... Variables declared with var at the top level become properties of the global object (window in browsers, global in Node.js), making them vulnerable to conflicts and overwrites.