Everything in JS is bound to containing scope. Therefore, if you define a function directly in file, it will be bound to window object, i.e. it will be global.

To make it "private", you have to create an object, which will contain these functions. You are correct that littering global scope is bad, but you have to put something in global scope to be able to access it, JS libraries do the same and there is no other workaround. But think about what you put in global scope, a single object should be more than enough for your "library".

Example:

MyObject = {
    abc: function(...) {...},
    pqr: function(...) {...}
    // other functions...
}

To call abc for somewhere, be it same file or another file:

MyObject.abc(...);
Answer from Marko Gresak on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_scope.asp
JavaScript Scope
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... Scope determines the accessibility (visibility) of variables. ... Variables declared Globally (outside any block or function) have Global Scope. Global variables can be accessed from anywhere in a JavaScript ...
🌐
W3Schools
w3schools.com › jsref › jsref_obj_global.asp
JavaScript Global Reference
Since these methods are global, and in a web browser the global object is the browser window, these methods are actually window methods: ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Subinsb
subinsb.com › global-functions-javascript
Create Global Functions In JavaScript - Subin's Blog
To make the "checkCookie" function global, we are going to add the function to the "window" object :
🌐
Contentful
contentful.com › blog › the-global-object-in-javascript
What is the global object in JavaScript? A practical guide for developers | Contentful
March 14, 2024 - Here’s how to use globalThis to set and access global variables and functions in browser windows, Node.js, and in workers: ... globalThis calls this from the global scope, returning the global object, even when you’re not in the global scope. This is much simpler than having a different behavior for different environments and scopes; however, it’s important to understand how to access the global object, and how scopes work without globalThis, if you want to be able to effectively write JavaScript code.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Global object
var gVar = 5; alert(window.gVar); // 5 (became a property of the global object) Function declarations have the same effect (statements with function keyword in the main code flow, not function expressions). Please don’t rely on that! This behavior exists for compatibility reasons. Modern scripts use JavaScript modules where such a thing doesn’t happen.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Functions
Functions - JavaScript | MDN
Functions form a scope for variables—this means variables defined inside a function cannot be accessed from anywhere outside the function. The function scope inherits from all the upper scopes. For example, a function defined in the global scope can access all variables defined in the global scope.
Find elsewhere
🌐
Quora
quora.com › What-is-the-global-function-in-JavaScript
What is the global function in JavaScript? - Quora
Answer (1 of 2): The global object in JavaScript is an always defined object that provides variables and functions, and is available anywhere. In a web browser, the global object is the window object, while it is named global in Node. js. The global object can be accessed using the this operator ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Global_object
Global object - Glossary | MDN
In JavaScript, the global object always holds a reference to itself: ... console.log(globalThis === globalThis.globalThis); // true (everywhere) console.log(window === window.window); // true (in a browser) console.log(self === self.self); // true (in a browser or a Web Worker) console.log(frames ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects
Standard built-in objects - JavaScript | MDN
For more information about the distinction between the DOM and core JavaScript, see JavaScript technologies overview. These global properties return a simple value. They have no properties or methods. ... These global functions—functions which are called globally, rather than on an object—directly return their results to the caller.
🌐
Medium
medium.com › @happymishra66 › object-global-function-in-javascript-d6ad5c5a3df3
Object — Global function in JavaScript | by Rupesh Mishra | Medium
March 19, 2023 - All objects in JavaScript, inherit all the properties and methods of the Object.prototype. ... All the above three object a, b and c have the same constructor function which show that they are created using the same constructor function i.e Object().
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Global Scope and Functions - JavaScript - The freeCodeCamp Forum
Hi there, I am currently doing JS Data Structures and Algorithms → Global Scope and Functions It says: “In JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your ...
Published   July 17, 2023
🌐
W3docs
w3docs.com › javascript
How to Define Global Variable in a JavaScript Function
Alternatively, assign the property to a window because, in JavaScript, you can define global variables inside a function using the window object.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Function
Function - JavaScript | MDN
... // Create a global property with `var` var x = 10; function createFunction1() { const x = 20; return new Function("return x;"); // this `x` refers to global `x` } function createFunction2() { const x = 20; function f() { return x; // this `x` refers to the local `x` above } return f; } ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-declare-global-variables-in-javascript
How to declare Global Variables in JavaScript ? - GeeksforGeeks
July 23, 2025 - ... // Declare global variables outside of any function or block scope var globalVar1 = "Hello"; let globalVar2 = "World"; const globalVar3 = "!"; Example 1: Declaring Global Variables in JavaScript