Dynamic import is a function-like expression in JavaScript that asynchronously loads an ECMAScript module at runtime. Unlike static import declarations—which are statically analyzed, resolved, and linked into a dependency graph prior to execution—dynamic imports return a Promise and can be executed conditionally or on-demand within any lexical scope. While the actual evaluation of the module code happens at runtime for both, static imports are evaluated strictly before the importing module executes, whereas dynamic imports are evaluated exactly when theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
import() expression is invoked.
Syntax
moduleSpecifier: A string, or an expression that evaluates to a string, representing the path to the module.options(Optional): An object containing import options. This is standardly used for Import Attributes to specify the module format or type.
Technical Mechanics
When the JavaScript engine encounters animport() expression, it initiates the fetch, instantiation, and evaluation of the target module asynchronously.
1. The Module Namespace Object and Live Bindings
The Promise returned by import() resolves to a module namespace object. Crucially, this object does not act as a static snapshot; it provides live bindings to the exports of the requested module. If an exported variable is updated from within the loaded module, the new value is immediately reflected on the module namespace object returned by the dynamic import.
default is a reserved keyword in JavaScript, you must alias it when destructuring the module namespace object:
moduleSpecifier does not need to be a static string literal. It can be a dynamically computed value, such as a variable or a template literal.
import statements are strictly constrained to the top level of a module to allow the JavaScript engine to statically analyze the dependency graph and create Module Records before execution. Dynamic import() bypasses this constraint and can be placed inside functions, if statements, try/catch blocks, or loops.
Static vs. Dynamic Import Comparison
| Characteristic | Static import | Dynamic import() |
|---|---|---|
| Resolution & Linking | Pre-execution (Parse/Link phase) | Runtime |
| Evaluation Phase | Runtime (Strictly ordered before importing module) | Runtime (Upon expression execution) |
| Return Value | None (Declarative syntax) | Promise<ModuleNamespaceObject> |
| Binding Mechanism | Live bindings | Live bindings (via the resolved namespace object) |
| Specifier | Strict string literal | Any expression evaluating to a string |
| Placement | Top-level only | Anywhere (functions, blocks, etc.) |
| Environment | Modules only (type="module") | Modules and standard Scripts |
Caching Behavior
The JavaScript engine maintains a module map. Ifimport() is called multiple times with the same resolved moduleSpecifier, the engine fetches, instantiates, and evaluates the module only once. Subsequent calls return a Promise that resolves immediately with the already-cached module namespace object.
Master JavaScript with Deep Grasping Methodology!Learn More





