how to declare and initialize global arrays in javascript

advertisements

In my html code, I need a global array which I use throughout my application. How to create and initialize global array variable in javascript..


You can do it several ways :
In the global scope :

var arr = [];

Binding the array to the global namespace:

window.arr = [];

or, for running the code in other environments, where the global object is not necessarily called window:

(function(global){
    global.arr = [];
})(this);