Avoid global variable for callback function called by function with no arguments
You can avoid the global by creating a closure around the function to be called by the maps api code. To do this define another function that returns a function. You can pass in the callback here.
The below code is runnable in single file but you will need to split it across pages in your real code.
function createInitMap(callback){
    return () =&gt; {
        const mapOptions = {
            center: {lat: 0, lng: 0},
            zoom: 7
        };
        window.map = new google.maps.Map(document.getElementById(&quot;map&quot;), mapOptions);
    
        if (typeof callback !== 'undefined') {
            callback();
        }        
    }
}

/*
    In first page
*/
//&lt;script type=&quot;text/javascript&quot; src=&quot;js/index.js&quot;&gt;&lt;/script&gt;
let initMap = createInitMap(() =&gt; console.log('first callback'));
//&lt;script type=&quot;text/javascript&quot; src=&quot;js/maps.js&quot;&gt;&lt;/script&gt;

initMap(); // simulate google maps calling your function

/*
    In second page
*/
//&lt;script type=&quot;text/javascript&quot; src=&quot;js/index.js&quot;&gt;&lt;/script&gt;
initMap = createInitMap(() =&gt; console.log('second callback'));
//&lt;script type=&quot;text/javascript&quot; src=&quot;js/maps.js&quot;&gt;&lt;/script&gt;

initMap();  // simulate google maps calling your function

Ensure the return value of the function is assigned to the global name initMap (It might need to be window.initMap).