In native JavaScript before ES6 Modules 2015 has been introduced had no import, include, or require, functionalities. Before that, we can load a JavaScript file into another JavaScript file using a script tag inside the DOM that script will be downloaded and executed immediately.
Now after the invention of ES6 modules there are so many different approaches to solve this problem have been developed and discussed below.
ES6 Modules: ECMAScript (ES6) modules have been supported in Node.js since v8.5. In this module, we define exported functions in one file and import them in another example.
There are two popular way to call a JavaScript file from another function those are listed below:
- Ajax Techniques
- Concatenate files
Ajax Techniques Example:
- External JavaScript file named as “main.js”
// This alert will export in the main filealert("Hello Geeks")chevron_rightfilter_none - Main file: This file will import the above “main.js” file
<!DOCTYPE html><html><head><title>Calling JavaScript file fromanother JavaScript file</title><script type="text/javascript">varscript = document.createElement('script');script.src =document.head.appendChild(script)</script></head><body></body></html>chevron_rightfilter_none - Output:

Concatenate files Example: Here importing multiple JavaScript files into a single JavaScript file and calling that master JavaScript file from a function.
- External JavaScript file named as “main.js”
// This alert will export in the main filealert("Hello Geeks")chevron_rightfilter_none - External JavaScript file “second.js”
// This alert will export in the main filealert("Welcome to Geeksforgeeks")chevron_rightfilter_none - External JavaScript file “master.js”
functioninclude(file) {varscript = document.createElement('script');script.src = file;script.type ='text/javascript';script.defer =true;document.getElementsByTagName('head').item(0).appendChild(script);}/* Include Many js files */chevron_rightfilter_none - Main file: This file will import the above “master.js” file
<!DOCTYPE html><html><head><title>Calling JavaScript file fromanother JavaScript file</title><script type="text/javascript"</script></head><body></body></html>chevron_rightfilter_none - Output:
main.js file import:

second.js file import:

Recommended Posts:
- How to include one CSS file in another?
- What is the best way to include CSS file? Why use @import?
- How to include inline JavaScript in HAML ?
- PHP | (Include and Require)
- How to include() all PHP files from a directory ?
- Include Bootstrap in AngularJS using ng-bootstrap
- AngularJS | ng-include Directive
- SASS | @mixin and @include
- Include v/s Extend in Ruby
- Why does AngularJS include an empty option in select ?
- @include vs @extend in SASS
- How to include Functions from other files in Node.js ?
- How to include Search box in your wordpress website ?
- How to copy a file from one directory to another using PHP ?
- Inherit a class to another file in Sass
- JavaScript | WebAPI | File | File.type Property
- JavaScript | WebAPI | File | File.size Property
- JavaScript | WebAPI | File | File.name Property
- Remove the Chrome's "No file chosen option" from a file input using JavaScript
- How to move an array element from one array position to another in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


