How to Dynamically Load JS inside JS? Last Updated : 14 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In JavaScript, dynamically loading scripts allows the inclusion of external JavaScript files only when needed, improving performance and efficiency. Instead of using static imports, scripts can be loaded conditionally at runtime. ApproachES6 provides us with a construct import(), which provides the facility to import a module on demand.import() returns a promise to provide a module object of the requested module.We can utilize the module object for using the various imports.Syntax:import("#ModuleSource").then((module)=>{ //use module object to access any of the imports. }) Example: Let us say we want to run a script to perform some operation on string depending on the button clicked. HTML <!-- index.html:- contains frontend scripts --> <!DOCTYPE html> <html> <head> <title>String operations</title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <input type="text" id="myString"> <button id="reverse" style="padding: 0.5em 0.5em;"> Reverse the String !! </button> <button id="palindrome" style="padding: 0.5em 0.5em;"> Check whether palindrome !! </button> <div id="answer" style="color: green; font-size: 2em; padding: 1em;"> </div> <!-- Script to perform one of the operations. --> <script type="text/javascript"> let reverseButton = document.getElementById("reverse"); let palindromeButton = document.getElementById("palindrome"); //module containing the logic to reverse a string. let module1 = '/reverseString.mjs'; //module containing the logic to check //whether the string is palindrome or not. let module2 = '/isPalindrome.mjs'; reverseButton.addEventListener("click", () => { //consuming the value of input let str = document.getElementById("myString").value; import(module1).then(module => { document.getElementById("answer").innerHTML = module.reverseString(str); }); }); palindromeButton.addEventListener("click", () => { //consuming the value of input let str = document.getElementById("myString").value; import(module2).then(module => { if (module.isPalindrome(str)) { document.getElementById("answer").innerHTML = "The string is a palindrome"; } else { document.getElementById("answer").innerHTML = "The string is not a palindrome"; } }); }); </script> </body> </html> javascript // reverseString.mjs // module to reverse a given string export function reverseString(str){ return str.split('').reverse().join(''); } JavaScript // isPalindrome.mjs // module to check whether string is palindrome or not export function isPalindrome(str){ return (str===str.split('').reverse().join('')) } Output:Note: The modules can be dynamically loaded inside regular scripts also.A local server needs to be set up to avoid cross site origin issue while using ES6 modules.When to use what?Dynamic imports are useful when there is some module which is seldom required in the script. This improves the performance at the initial load time. But if any exports are used frequently within a script, then it can cause some lag during execution. Comment More infoAdvertise with us Next Article How to Dynamically Load JS inside JS? S SauravVirmani Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2019 ES6 JavaScript-Questions +2 More Similar Reads How to dynamically load module in React.js ? In React JS loading of modules statically is cumbersome as it loads the modules beforehand even if it is not rendered. Some components render only when the user interacts, so in such cases, it can lead to more resource consumption. When you statically import modules, you are loading larger data than 3 min read How to load CSS and JS files dynamically ? Generally, the CSS and JS files are included statically with HTML code. It means they are written in the script or the link tag in the  HTML code. But this slows down the execution as a bulk of code is loaded unnecessarily. It may or may not use the functionality related to that DOM element. So dyna 5 min read How to Load External JS Scripts Dynamically in AngularJS ? The task is to load a JS code either from a CDN or JS file stored in the system dynamically at runtime in AngularJS i.e. JavaScript code should be loaded dynamically. For example, on a button press or on some event.Whenever we want to load the script file we would have to create an element of type â 2 min read How to include inline JavaScript in HAML ? There are mainly two ways to include JavaScript in HTML: Internal/Inline: By using "script" element in the "head" or "body" section. External: By using an external JavaScript file. We will be extending the below example for HTML and see how it could be implemented similarly for HAML. To include inli 2 min read How to Use Dynamic Variable Names in JavaScript? Dynamic variable names are variable names that are not predefined but are generated dynamically during the execution of a program. This means the name of a variable can be determined at runtime, rather than being explicitly written in the code.Here are different ways to use dynamic variables in Java 2 min read Like