CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

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.

JAVASCRIPT CODE
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.

JAVASCRIPT CODE
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.

Preview

Output

JAVASCRIPT CODE
Example 1 - dynamic import in JavaScript
The imported value is 11

Example 2

Filename: test.js

In the code below, we have defined the add() function, which returns the sum of two numbers.

JAVASCRIPT CODE
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.

Preview

Output

JAVASCRIPT CODE
Example 2 - dynamic imports in JavaScript
The sum of 2 and 3 is 5

This 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.