Sometimes, we want to dynamically load a JavaScript file.
In this article, we’ll look at how to dynamically load a JavaScript file.
How to dynamically load a JavaScript file?
To dynamically load a JavaScript file, we can call the import
function to import a module dynamically.
For instance, we write
(async () => {
const bar = await import("./synth/BubbleSynth.js");
})();
to call import
to import ./foo/bar.js
dynamically.
It returns a promise with the module.
And we use await
to get the content of the module.
Conclusion
To dynamically load a JavaScript file, we can call the import
function to import a module dynamically.