Javascript

Can we call the function written in one JavaScript in another JS file

25 September 2026 · 7 min read

Can we call the function written in one JavaScript in another JS file

One of the most common questions for developers embarking on their JavaScript journey, or even seasoned pros refactoring older codebases, is: can we call the function written in one JavaScript in another JS file? The answer is a resounding yes, and understanding how to achieve this is fundamental to writing clean, maintainable, and scalable web applications. Modern web development heavily relies on breaking down complex applications into smaller, manageable pieces, and effectively communicating between these pieces is paramount. This capability allows for better organization, promotes code reusability, and significantly improves the collaborative development experience.

Historically, sharing JavaScript functions across files involved some clever workarounds and an understanding of the global scope. However, with the advent of modern JavaScript features, particularly ES Modules, the process has become much more explicit and robust. This article will delve into the various methods, from traditional approaches to the best practices recommended today, providing you with a comprehensive guide to seamless JavaScript function sharing across different files. We’ll explore the underlying principles that make this possible and demonstrate why modular programming is the cornerstone of efficient front-end engineering.

The Fundamentals of JavaScript Scope and File Separation

Before diving into how to call functions across different JavaScript files, it’s crucial to grasp the concept of scope within JavaScript. Scope dictates the accessibility of variables, functions, and objects in different parts of your code. When you define a function or a variable in a JavaScript file, its accessibility depends on where and how it’s declared. Understanding this is key to appreciating why certain methods for file communication work, and why others fall short.

In a traditional browser environment, each JavaScript file loaded via a <script> tag typically executes in its own context, but variables and functions declared at the top level (outside of any function) implicitly become part of the global scope. This means they attach to the window object in browsers, making them accessible from any other script that executes after them. While seemingly convenient, relying heavily on the global scope can lead to “global namespace pollution,” where different scripts might accidentally overwrite each other’s variables or functions, leading to unpredictable behavior and difficult-to-debug issues. This is a primary reason why modern JavaScript development discourages extensive use of the global scope for sharing assets.

Global vs. Local Scope

Variables and functions declared directly within a JavaScript file, outside of any specific function, exist in the global scope. This makes them available to all other scripts loaded on the same page. For example, if script1.js defines function myGlobalFunction() {}, then script2.js can simply call myGlobalFunction(). This simplicity, however, comes with significant drawbacks. As applications grow, managing these global dependencies becomes a nightmare. Conflicts can easily arise when multiple scripts define functions or variables with identical names, leading to unexpected behavior as the last script loaded will typically overwrite previously defined global identifiers.

In contrast, variables and functions declared inside another function are confined to that function’s local scope. They are not accessible from outside that function. This principle of encapsulation is vital for creating robust and isolated code blocks. Modern JavaScript practices leverage this concept to create private variables and functions, exposing only what is absolutely necessary to the outside world. The move towards modularity is fundamentally about moving away from implicit global dependencies to explicit imports and exports, providing clearer control over what parts of your code are accessible where.

Traditional Methods for Script Communication

Before ES Modules became the standard, developers employed several techniques to allow functions defined in one JavaScript file to be called in another. These methods, while still functional, often came with limitations and challenges, particularly in larger projects. They relied heavily on the sequential loading of scripts and careful management of the global object. Understanding these older methods provides valuable context for appreciating the elegance and robustness of modern module systems.

One of the most straightforward ways to share functions was by ensuring that the script defining the function was loaded before the script that needed to call it. This often meant meticulous ordering of <script> tags in the HTML. For example, a utility file containing common helper functions would always be loaded first. Another common pattern involved immediately invoked function expressions (IIFEs) to encapsulate code and prevent global pollution, while still selectively exposing certain functions to the global scope or to specific objects.

Script Tag Order

The most basic way to enable function calls across files involves the order in which your JavaScript files are included in your HTML document. When you link JavaScript files using <script src="path/to/script.js"></script> tags, the browser executes them in the order they appear. If fileA.js contains a function that fileB.js needs to call, then fileA.js must be loaded before fileB.js. This ensures that the function is defined in the global scope by the time fileB.js attempts to access it. This method is simple but quickly becomes unmanageable in complex applications with many dependencies, as changing the order can break functionality.

<!-- fileA.js defines mySharedFunction --> <script src="fileA.js"></script> <!-- fileB.js calls mySharedFunction --> <script src="fileB.js"></script> 

This approach works for simple cases, but managing dependencies manually can become a nightmare. Imagine an application with dozens of JavaScript files, each with its own set of interdependencies. Developers would spend significant time just ensuring the correct script loading order, and a single mistake could lead to runtime errors. This dependency on implicit global variables and strict loading order made code hard to refactor and debug, highlighting the need for more explicit dependency management.

IIFEs and the Global Object

Immediately Invoked Function Expressions (IIFEs) were a popular pattern to create a private scope for variables and functions, thereby preventing global namespace pollution. An IIFE is a function that runs as soon as it is defined. While it creates its own scope, developers could still selectively expose certain functions or objects to the global scope (e.g., attaching them to the window object) to make them accessible to other scripts. For instance, a library could wrap all its code in an IIFE and then expose only its main API function to the global window object.

This technique offered a significant improvement over simply dumping everything into the global scope. It allowed developers to encapsulate internal logic while still providing a controlled interface for other scripts to interact with. However, it still relied on the global object as the intermediary, which isn’t ideal for large-scale applications. It also required careful manual management of dependencies and often led to bulky global objects, contradicting the principles of clean, modular code. According to a report by The State of JS, modern module systems are overwhelmingly preferred for new projects due to their superior dependency management and tree-shaking capabilities, which IIFEs cannot replicate effectively. The State of JS 2023 Survey provides insights into current trends.

Modern JavaScript Modules (ES Modules)

The most robust and recommended way to call functions from one JavaScript file in another today is by using ECMAScript Modules, often simply called ES Modules. This feature was officially introduced in ES2015 (ES6) and provides a standardized system for organizing JavaScript code into reusable units. ES Modules allow you to explicitly define what parts of a file (functions, variables, classes) are available for use in other files, and what parts are private. This explicit declaration significantly improves code organization, readability, and Question & Answer :

Can we call the function written in one JS file in another JS file? Can anyone help me how to call the function from another JS file?

The function could be called as if it was in the same JS File as long as the file containing the definition of the function has been loaded before the first use of the function.

I.e.

File1.js

function alertNumber(number) { alert(number); } 

File2.js

function alertOne() { alertNumber("one"); } 

HTML

<head> .... <script src="File1.js" type="text/javascript"></script> <script src="File2.js" type="text/javascript"></script> .... </head> <body> .... <script type="text/javascript"> alertOne(); </script> .... </body> 

The other way won’t work. As correctly pointed out by Stuart Wakefield. The other way will also work.

HTML

<head> .... <script src="File2.js" type="text/javascript"></script> <script src="File1.js" type="text/javascript"></script> .... </head> <body> .... <script type="text/javascript"> alertOne(); </script> .... </body> 

What will not work would be:

HTML

<head> .... <script src="File2.js" type="text/javascript"></script> <script type="text/javascript"> alertOne(); </script> <script src="File1.js" type="text/javascript"></script> .... </head> <body> .... </body> 

Although alertOne is defined when calling it, internally it uses a function that is still not defined (alertNumber).