Global functions in javascript

I’m new to js and trying to understand global and private functions. I understand global and local variables. But if I have an html named test.html and a 2 js files named test1.js and test2.js. Now I include the test1.js and test2.js in test.html and call the functions written in test2.js inside test1.js and test.html.

The functions that I have written in test2.js are in this form

function abc(){...}

function pqr(){...} etc.

are these above functions global? If they are , how can I not make them global and still access them in test1.js and test.html?

As I have read global functions or global variables are bad right?

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”.


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

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

Anything defined in a file without any sort of wrapper will be bound to the window object. Anything bound to the window object is global.


//these are global variables
foo = 123;
var ABC = 'school';

//these are "private" variables
test = function(){
  var foo = 123

  var ABC = 'school';

Since global variables in every file will be part of the window object, you can access them between files. It is important when creating “private” variables you add var. This says override any global variables in the current “wrapper”. If I have a global variable foo and I define it again in a function with var they will be separate.

var foo = 123;
  var foo = 987; //this foo is separate from the above foo

If you do have a “wrapper” and you want to define a global function you can do it like this: = 123;
(function(){ = 123;

Both functions will do the same thing.

Personally, I prefer to put everything in a wrapper and only define global variables when I need them using window.


  //all code goes here

  //define global variable = 123;


var SomeName = function() {

    var function1 = function (number) {
        return number+1;

    var anotherFunction = function (number) {
        return number+2;

    return {
        function1: function (number) {
            return function1(number);
        function2: function (number) {
            return anotherFunction(number);


console.log(SomeName.function1(1)); //logs 2

console.log(SomeName.function2(1)); //logs 3

A modern approach (2020) to using global data is by using a global object literal and define there all your needed logic.

const Website = {
  foo () {
  bar () {

document.addEventListener('DOMContentLoaded', () => {

If your code is more complex than a couple of lines you will need to separate your code into multiple files and with webpack you merge them together into one file.

Read More:   How can I add a keyboard shortcut to an existing JavaScript Function?

import Foo from './js/Foo.js'
import Bar from './js/Bar.js'

// define here another object literal or setup document events.
// webpack will merge all this together into one file
<script src=""></script>

The reasons you don’t want to import individual js files with html is described here. The jist of it is you will have poor performance, so you must bundle all your js.

If you don’t understand why global variables are bad, then why are you trying to avoid them?

Global functions aren’t necessarily bad. What’s bad is state that anyone and anything and change.

In general since you’re new to Javascript, it’s fine to start out with global functions spread out across multiple javascript files that you include in your html file via script tags.

As you transition from beginner to intermediate, you will have to look into some “module” solution (I personally recommend RequireJS).

For now though, you can make do with a simpler module pattern:

var T1 = function() {
   return < some module object >
})(); // notice the parenthesis

Google “Javascript module pattern”.

Also see this answer.

vos fonctions ne sont pas global si vous faite l’erreur de les incorporé dans par exemple :

$( document ).ready(function() {  
function myglobalfunction() { ... }

vous devez retirer

$( document ).ready(function() {

your functions are not global if you make the mistake of embedded them in for example:

$(document) .ready (function () {
function myglobalfunction () {

you must remove

$ (document) .ready (function () {

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts