DEV Community

buginit
buginit

Posted on

7 Examples Of JavaScript Dynamic Import ES10

In this article, we will talk about JavaScript Dynamic Import that introduced in ES10/2019, we will cover basics and different types of examples also we will take a look at static and Dynamic Import in JavaScript. And don’t worry I’m not going to give you a long lecture, we will cover this with small points and examples.

Let’s start, Basically dynamic import is a new function like import statement that introduced in ES10/2019 and it returns a promise. As we know let’s have a look at static import of JavaScript below:

import MyModule from './my-module.js';
import { NamedExport } from './other-module.js';

Now, Let’s have a look at the basic JavaScript dynamic import statement.

#1 Simple Single Line Example

import("module/foo.js").then(foo => console.log(foo.default))

In the above example, we used the single-line statement of Arrow function for console.log to make it shorter. You can also use the normal function like the below example:-

import('module/foo.js').then(function(foo) {
  console.log(foo.default);
})

To Read full 7 Examples Of JavaScript Dynamic Import ES10 click here

Top comments (0)