New is not a keyword.

Use:

var data = new Array();

Or, more succinctly:

var data = [];

After your edit you mention that the first script block is loaded asynchronously. Your code will not work as written. data is a global variable, once it is loaded onto the page. You need to use a callback pattern to properly execute the code.

Since you haven't posted the asynchronous code I am not going to provide a callback sample. Though, a quick solution follows:

var interval = setInterval(function(){
    if(data) {
        /* ... use data ... */
        clearInterval(interval);
    }
}, 500);
Answer from Alex on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
The this value ultimately observable ... with globalThis. The thisArg argument is irrelevant for any callbackFn defined with an arrow function, as arrow functions don't have their own this binding. The array argument passed to callbackFn is most useful if you want to read another index during iteration, because you may not always have an existing variable that refers ...
🌐
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.
🌐
Codecademy
codecademy.com › forum_questions › 55a0322c9113cb6349000652
Why can a function create and store an array object, but not a global variable? | Codecademy
var a; function foo(){ a = true; } foo(); if (a) console.log("declared globally, defined locally"); Aside: A reference object such as an array or plain object retains its scope even when passed as an argument to a function. The local variable set out for it in the parameter will point to the object in that scope.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Grammar_and_types
Grammar and types - JavaScript | MDN
An array literal creates a new array object every time the literal is evaluated. For example, an array defined with a literal in the global scope is created once when the script loads.
🌐
Javatpoint
javatpoint.com › javascript-global-variable
JavaScript global variable - javatpoint
Primitive data type Non-primitive (reference) data type JavaScript is a dynamic type language, means you don't need to specify type of the variable because it is dynamically... ... JavaScript Function apply() method The JavaScript Function apply() method is used to call a function contains this value and an argument contains elements of an array.
🌐
Hostman
hostman.com › tutorials › understanding global variables in javascript
Understanding Global Variables in JavaScript | Hostman
December 26, 2025 - Its index will be N-1. In our case, it will equal 1. To change the value of an element, you need to assign a new value to the variable, as shown below: var EmployeeArr = new Array(‘Alex’, ‘Bob’, ‘Oliver’);EmployeeArr[1] = ‘Jack’; Now EmployeeArr contains 'Alex', 'Jack', and 'Oliver.' Now let's look at length, an important property that returns the length of an array in JavaScript.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
Find elsewhere
🌐
Snook
snook.ca › archives › javascript › global_variable
Global Variables in JavaScript - Snook.ca
Whether it's the widget or a "global" that holds the array, one of these will need to be "global." Finally, I'm using "global" in quotes because it's not really global. As this page says, your new variable becomes part of the window object. ... Global variables are hard too avoid in JavaScript.
🌐
GDevelop
forum.gdevelop.io › how do i...?
Declaring global variable as an array and accesing instance variables through javascript - How do I...? - GDevelop Forum
August 4, 2019 - How does it work? I tried making a global variable an array by putting ‘[]’ around it but it doesn’t work. I also can’t access instance variables by object.variableName or object.getVariables.get(“name”). If global var…
🌐
GeeksforGeeks
geeksforgeeks.org › global-and-local-variables-in-javascript
Global and Local variables in JavaScript | GeeksforGeeks
Variables created outside of functions are global variables, and the code in all functions has access to all global variables. If you forget to code the var keyword in a variable declaration, the JavaScript engine assumes that the variable is global.
Published   August 20, 2024
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Global object
The global object holds variables that should be available everywhere. That includes JavaScript built-ins, such as Array and environment-specific values, such as window.innerHeight – the window height in the browser.
🌐
freeCodeCamp
forum.freecodecamp.org › t › push-local-variable-to-global-array › 67616
Push local variable to global array? - The freeCodeCamp Forum
September 5, 2016 - I’m stuck on something that seems really basic but I haven’t been able to find a solution. This is what my code setup looks like: var myArray = [1,2,3]; $(’#submitButton’).submit(function({ myArray.push(4); console.log(myArray); }) My goal is that every time someone submits on ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Accessing global array using "this" in prototype function - JavaScript - The freeCodeCamp Forum
September 1, 2020 - Hello js people, Trying to solve a problem related to JS functional programming. The solution is given below: // The global variable var s = [23, 65, 98, 5]; Array.prototype.myFilter = function(callback) { // Only change code below this line console.log(this); //this prints [ 23, 65, 98, 5 ] var newArray = []; this.forEach(val => { if(callback(val) == true){ newArray.push(val) } }); // Only change code above this line return newArray; }; var new_s = s.myFilter(fun...
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_global_variables.htm
JavaScript - Global Variables
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 id = "demo"> </p> <script> const output = document.getElementById("demo"); function test() { a = 90; output.innerHTML += "a -> Inside the function = " + a + "<br>"; } test(); output.innerHTML += "a -> Outside the function = " + a + "<br>"; </script> </body> </html>
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];
    ...
    ...
}
🌐
Tabnine
tabnine.com › home › global variable in javascript
Global Variable in JavaScript - Tabnine
July 25, 2024 - The following examples explore working with global variables and scopes. ... The following declaration takes place outside of a function’s scope, meaning the toys array is a global variable:
🌐
Stack Overflow
stackoverflow.com › questions › 23283199 › making-an-array-variable-global-in-javascript
jquery - Making an Array Variable Global in JavaScript - Stack Overflow
if (localStorage.getItem("posts") === null) { var postArray = new Array(); } I'm trying to make the postArray global at the start yet only create a new array when localStorage is empty.