JavaScript - Dynamic Imports
JavaScript - Dynamic Imports
Dynamic Imports
Whenever the application code grows and contains thousands of lines, it is important to split the code into modules. Modules allow developers to break down complex codes and use them in different files by importing.
In JavaScriptmoduleschapter, you learned to import and export objects from the modules, but it was a static import, as we load them at the top of the code. Sometimes, we need to import the modules whenever needed rather than importing them statically to improve the performance of the web application. It is also calleddynamic imports.
The import() Expression for Dynamic Imports
Whenever you need to import a module dynamically, you can use theimport()expression, which returns the promise. We can import the module dynamically using the import() expression anywhere in the code.
Syntax
Users can follow the syntax below to import modules dynamically using the import() expression.
import(moduleName).then(module => {
// Use the module here.
});In the above syntax, we use the then() method with the import() expression to resolve the promise.
Parameters
- moduleNameβ It takes a module name or path as a parameter, which we need to load dynamically.
Return Value
It returns the promise.
Examples of Dynamic Import
Example 1
Filename: test.js
In the code below, we export the βval1β variable containing the integer value.
export const val1 = 11;Filename: test.html
In the code below, we have used the if-else statement. We import the module and use its objects in the if() block.
This way, we can import modules dynamically using the import() expression.
Output
Example 1 - dynamic import in JavaScript
The imported value is 11Example 2
Filename: test.js
In the code below, we have defined the add() function, which returns the sum of two numbers.
export function add(a, b) {
return a + b;
}Filename: test.html
In the code below, we have added the βclickβ event listener to the button. When a user clicks the button, it loads the βtest.jsβ module. Here, we used the async/await to handle the promise returned by the import() expression.
After that, we use the add() function of the module to sum two numbers, and users can observe the summation result in the output.
Output
Example 2 - dynamic imports in JavaScript
The sum of 2 and 3 is 5This way, developers can import modules dynamically in the function, if-else block, etc., whenever needed rather than importing them at the top of the code.