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 › 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
🌐
W3Schools
w3schools.com › js › js_scope.asp
JavaScript Scope
All scripts and functions in the same web page can access a variable with global scope. Each JavaScript function have their own scope.
🌐
Star Oceans
staroceans.org › w3c › jsref_obj_global.asp.html
JavaScript Global Reference
The JavaScript global properties and functions can be used with all the built-in JavaScript objects. It makes sense to call the list above global functions rather than global methods because the functions are called globally and not any objects.
🌐
Medium
medium.com › @happymishra66 › object-global-function-in-javascript-d6ad5c5a3df3
Object — Global function in JavaScript | by Rupesh Mishra | Medium
March 19, 2023 - Object — Global function in JavaScript JavaScript has so many global functions and objects. Detailed list can be found here. Object is one such global constructor function in JavaScript which is …
🌐
W3Schools
w3schools.com › js › js_function_this.asp
JavaScript this in Functions
In a regular function in strict mode, when used outside a function, this also refers to the global object: ... In both the examples above, this is the global object. It is so because , this is in the global scope. In JavaScript, globalThis is a special built-in object that provides a standard way to access the global object, regardless of the environment your code runs in - whether it is a browser, Node.js, a Web Worker, or another JS runtime.
🌐
OpenText
docs.microfocus.com › SM › 9.61 › Hybrid › Content › programming › javascript › reference › list_javascript_global_methods.htm
List: JavaScript global methods
Global methods are functions that are available to any script as they are not methods of any specific object. You can invoke global methods directly just as you would do with any core JavaScript global functions such as parselnt() or eval().
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Global object
July 1, 2022 - The global object provides variables and functions that are available anywhere.
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_function_intro.asp
JavaScript Functions
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... JS Numbers JS Number Methods JS Number Properties JS Number Reference JS Bitwise JS BigInt JS Functions
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects
Standard built-in objects - JavaScript - MDN Web Docs
This chapter documents all of JavaScript's standard, built-in objects, including their methods and properties. The term "global objects" (or standard built-in objects) here is not to be confused with the global 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 - Unfortunately, they are easy to misunderstand and misuse, leading to negative impacts on your applications’ functionality, performance, and security. This post will explain the global object in JavaScript, covering scopes, variables, and functions, and how you should safely use the global context.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_builtin_functions.htm
JavaScript Built-in Functions Reference
JavaScript - Function bind() JavaScript - Closures · JavaScript - Variable Scope · JavaScript - Global Variables · JavaScript - Smart Function Parameters · JavaScript Objects · JavaScript - Number · JavaScript - Boolean · JavaScript - Strings · JavaScript - Arrays ·
🌐
W3Schools
w3schools.com › jS › js_function_closures.asp
JavaScript Function Closures
Global variables can be made local (private) with closures. Closures makes it possible for a function to have "private" variables.
🌐
Subinsb
subinsb.com › global-functions-javascript
Create Global Functions In JavaScript - Subin's Blog
February 10, 2014 - To make the "checkCookie" function global, we are going to add the function to the "window" object :
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Global_object
Global object - Glossary - MDN Web Docs - Mozilla
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 === frames.frames); // true (in a browser) console.log(global === global.global); // true (in Node.js)
🌐
W3Schools
w3schools.com › js › js_functions.asp
JavaScript Function Study Path
A JavaScript callback is a function passed as an argument to another function, which is then executed (or "called back") at a later point in time to complete a specific task. ... How this works (especially inside objects). How this is decided by how it is called. ... Call a function with a chosen this. Pass a list of arguments.
🌐
W3Schools
w3schools.com › jsref › jsref_function.asp
JavaScript function Statement
A function can also be defined using an expression (See Function Definitions). Read our JavaScript Tutorial to learn all you need to know about functions. Start with the introduction chapter about JavaScript Functions and JavaScript Scope.
🌐
W3Schools
w3schools.com › js › js_function_expressions.asp
JavaScript Function Expressions
Callbacks Passing functions as arguments to other functions, such as event listeners or asynchronous operations. Closures Function expressions help create closures, which allow functions to remember and access variables from their containing scope. Immediately Invoked Function Expressions (IIFEs) Functions Expressions that run as soon as they are defined, are often used to create private scopes and avoid global variable conflicts.
🌐
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 blo…
Published   July 17, 2023