Is it possible to define a JavaScript global variable within a function?

Is it possible to declare a global variable in JavaScript within a function and access it in other functions? For example, if I declare and assign the trailimage variable inside the makeObj() function, can it be used in followmouse() or other functions?

Declare Global Variable Outside the Function In this approach, the trailimage variable is declared outside the makeObj function. The function assigns a value to this global variable.

var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;
var trailimage; // Declare the global variable here
function makeObj(address) {
trailimage = [address, 50, 50]; // Assign value to the global variable
document.write('<img id="trailimageid" src="' + trailimage[0] + '" border="0" style="position: absolute; visibility:visible; left: 0px; top: 0px; width: ' + trailimage[1] + 'px; height: ' + trailimage[2] + 'px">');
obj_selected = 1;
}

Another way to define a JavaScript global variable within a function is to explicitly attach the variable to the window object, which is the global scope in browsers.

var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;
function makeObj(address) {
window.trailimage = [address, 50, 50]; // Assign value to the global variable using the window object
document.write('<img id="trailimageid" src="' + window.trailimage[0] + '" border="0" style="position: absolute; visibility:visible; left: 0px; top: 0px; width: ' + window.trailimage[1] + 'px; height: ' + window.trailimage[2] + 'px">');
obj_selected = 1;
}

You can define a JavaScript global variable by declaring it without the var, let, or const keyword inside a function. This approach automatically attaches the variable to the global scope.

var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;
function makeObj(address) {
trailimage = [address, 50, 50]; // Assign value to the global variable without var, let, or const
document.write('<img id="trailimageid" src="' + trailimage[0] + '" border="0" style="position: absolute; visibility:visible; left: 0px; top: 0px; width: ' + trailimage[1] + 'px; height: ' + trailimage[2] + 'px">');
obj_selected = 1;
}