JavaScript function scope – going Global

In JavaScript, as in many other programming languages, variables have a scope – global or local / block.
Functions also have a scope, if they were defined inside other functions.
Here is an example:

Trying to use the “gtag” function outside the “loadAnalytics” function will not work. The function is limited in scope. In order to use it globally, the simplest solution would be to create a global variable and then assign this function to the variable.
Updated code:

Simple adjustment and now “gtag” is widely accessible!

Leave a Reply

Your email address will not be published. Required fields are marked *