> For the complete documentation index, see [llms.txt](https://demirels-organization.gitbook.io/javascript-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://demirels-organization.gitbook.io/javascript-tutorial/dynamic-imports.md).

# Dynamic Imports

Dynamic imports allow you to import and execute a module or a script on demand at runtime. This can be useful when you want to load a module or a script only when it is needed, instead of loading it upfront with the rest of your code.

Dynamic imports are written using the `import` keyword followed by a module or script path in parentheses, and they return a promise that is resolved with the module or script exports. Here's an example of how you can use a dynamic import to load a module on demand:

```javascript
async function loadModule() {
  const module = await import('./module.js');
  // Use the module
}
```

In the example above, the `loadModule` function uses a dynamic import to load the `module.js` module. The execution of the function is paused until the promise returned by the dynamic import is resolved, and the resolved value is stored in the `module` variable.

Dynamic imports can also be used to load scripts on demand. Here's an example of how you can use a dynamic import to load a script:

```javascript
async function loadScript() {
  const script = await import('./script.js');
  // The script has been executed
}
```

In the example above, the `loadScript` function uses a dynamic import to load the `script.js` script. The execution of the function is paused until the promise returned by the dynamic import is resolved, and the resolved value is stored in the `script` variable. The script will be executed when it is loaded.

Dynamic imports can be useful when you want to optimize the loading time of your application by only loading resources when they are needed. They can also be used to implement features like code splitting, which allows you to split your code into smaller bundles that can be loaded on demand.
