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
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_obj_global.asp
JavaScript Global Reference
Function apply() Function bind() Function call() Function length Function name Function toString() Function valueOf() JS Error ยท new Error() cause isError() name message JS Global
Discussions

Global Scope and Functions
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 ... More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
1
0
July 17, 2023
html - is it valid to use global function in javascript? - Stack Overflow
Global variables and functions can be overwritten by other scripts. Use local variables instead, and learn how to use closures. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Global scope functions in Javascript
greetings goes to all. could anyone give a simplified explanation about global scope in javascript> More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
1
1
May 13, 2020
Defining functions in global javascript tab in geogebra

first of all do developing in 5.x!

This is a standard msg if there is some error in code or data...

start with a simple alert-msg in global code

do developing in a click script - if running copy to global tab

More on reddit.com
๐ŸŒ r/geogebra
6
2
May 22, 2022
๐ŸŒ
OpenText
docs.microfocus.com โ€บ SM โ€บ 9.61 โ€บ Hybrid โ€บ Content โ€บ programming โ€บ javascript โ€บ reference โ€บ list_javascript_global_methods.htm
List: JavaScript global methods
July 23, 2025 - Therefore, a search for "cats" followed by a search for "Cats" would return the same number of Help topics, but the order in which the topics are listed would be different. ... 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().
๐ŸŒ
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 โ€ฆ
๐ŸŒ
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)
๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ advanced working with functions
Global object
July 1, 2022 - 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.
Find elsewhere
๐ŸŒ
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
๐ŸŒ
HCL Software
help.hcl-software.com โ€บ dom_designer โ€บ 9.0.1 โ€บ reference โ€บ r_wpdr_globals_r.html
Global objects and functions (JavaScript)
January 16, 2026 - Global functions can be called directly from server-side scripts. The following table lists the global functions.
๐ŸŒ
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 - Weโ€™ll explain how to use globalThis to access the global object later in this article โ€” but first, itโ€™s important to understand why it was necessary. ... scopes (also called contexts) determine where a variable or function that you have declared is accessible. Each scope has access to the functions and variables declared in its parent scopes. There is a precedence to how JavaScript resolves variables that appear inside scopes: if variables of the same name have been declared in multiple scopes, JavaScript uses the first one it finds, starting at the current scope, and working up toward the global scope.
๐ŸŒ
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 :
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
Flavio Copes
flaviocopes.com โ€บ javascript-global-object
The JavaScript Global Object
June 21, 2019 - JavaScript provides a global object which has a set of properties, functions and objects that are accessed globally, without a namespace.
๐ŸŒ
Reddit
reddit.com โ€บ r/geogebra โ€บ defining functions in global javascript tab in geogebra
r/geogebra on Reddit: Defining functions in global javascript tab in geogebra
May 22, 2022 -

Hi, I just startet scripting with javascript in geogebra and ran into different problems.

Is it possible to define global functions (inside the Global Javascript tab) that can be called from different objects (for example multiple buttons)? I tried it but didnยดt get my solution to work.

I have a text object ("text1") and a button object ("button1"). I defined a function ("doSomething") inside the Global Javascript tab. The function "doSomething" is called inside the On Click tab of button1. But when button1 is clicked, I get the error message "doSomething is not defined".

Top answer
1 of 10
96

In Chrome, go to Dev tools and open the console. Then type in the following:

Object.keys( window );

This will give you an Array of all the global variables.

After searching on Google a bit, I found a way. You will need Firefox and the jslinter addon.

Once setup, open jslinter and go to Options->check everything on the left column except "tolerate unused parameters".

Then run jslinter on the webpage and scroll down in the results. You will have a list of unused variables (global and then local to each function).

Now run Object.keys(window); in the console and compare the results from both to figure out which ones are used.

2 of 10
39

This one-liner will get you pretty close, and does not require installing anything additional, or running code before the page loads:

Object.keys(window).filter(x => typeof(window[x]) !== 'function' &&
  Object.entries(
    Object.getOwnPropertyDescriptor(window, x)).filter(e =>
      ['value', 'writable', 'enumerable', 'configurable'].includes(e[0]) && e[1]
    ).length === 4)

It filters Object.keys(window) based on three principles:

  1. Things that are null or undefined are usually not interesting to look at.
  2. Most scripts will define a bunch of event handlers (i.e. functions) but they are also usually not interesting to dump out.
  3. Properties on window that are set by the browser itself, are usually defined in a special way, and their property descriptors reflect that. Globals defined with the assignment operator (i.e. window.foo = 'bar') have a specific-looking property descriptor, and we can leverage that. Note, if the script defines properties using Object.defineProperty with a different descriptor, we'll miss them, but this is very rare in practice.
๐ŸŒ
Medium
olaishola.medium.com โ€บ understanding-javascript-scope-global-vs-function-scope-8f19cf107b25
Understanding JavaScript scope: Global vs. function scope | by Oladipupo Ishola | Medium
March 11, 2023 - Variable references in JavaScript are resolved using "scope chains," which are a series of nested scopes traversed by the interpreter to find the value of a variable. When the interpreter comes across a variable reference, it first checks the current scope, then the outer scope, and so on until it reaches the global scope. An error is thrown if the variable is not found in any of the scopes. ... function outer() { var x = 10; function inner() { var y = 20; console.log(x); // Output: 10 console.log(y); // Output: 20 } inner(); console.log(x); // Output: 10 console.log(y); // Uncaught ReferenceError: y is not defined }