Dynamically Loading JavaScript with in JavaScript Last Updated : 14 Oct, 2025 Comments Improve Suggest changes 1 Likes 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. How It Works:ES6 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. Create Quiz Comment S SauravVirmani Follow 1 Improve S SauravVirmani Follow 1 Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2019 ES6 JavaScript-Questions +2 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like