JS global variable doesn’t update

If you want to create global variable intentionally you need to do this.

window['your_variable_name']= 'Some variable value';

then you’ll be able to access it like window.your_variable_name – this way of creating and accessing global variable guarantees that you will work with same variable regardless of what build tools like webpack or from where you access it. For example you may need to access it from other function which in turn has same named variable defined, so without referencing your variable through window.variable_name notation you’ll access parent scope’s variable instead of global one.

Also if you need a global variable it’s name should be really unique and it’s a good practice to call it like you plugin name or something that will not be overwritten by some other’s code, avoid generic names like url.

So you can do this way window['YourUniquePluginGlobal'] = {url: 'your_url'};

Then you’ll be able to access your variables like window.YourUniquePluginGlobal.url

Do not rely on omitting var keyword.

And even more – NEVER omit var (or let, const in ES6) when you declare a variable, as it will make your code unreliable and unmaintainable.

Always use var, let \ const when you declare a variable in JS and may the good code be with you 🙂