Dynamic imports in JavaScript.

Following is the code for dynamic imports in JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .result {
      font-size: 18px;
      font-weight: 500;
      color: rebeccapurple;
<h1>Dynamic imports in JavaScript.</h1>
<div class="result"></div>
<button class="Btn">CLICK HERE</button>
Click on the above button to dynamically import modules
   document.querySelector(".Btn").addEventListener("click", loadModule);
   let resEle = document.querySelector(".result");
   async function loadModule() {
      let test = await import("./sample.js");
      resEle.innerHTML += test.testImport() + "<br>";
      resEle.innerHTML += test.tellTime();


The above code will produce the following output −

On clicking the ‘CLICK HERE’ button −