how to import javascript in javascript code example

Example 1: javascript using another js file

// module.js
export function hello() {
  return "Hello";
}

// main.js
import {hello} from 'module'; // or './module'
let val = hello(); // val is "Hello";

Example 2: javascript import

import { module } from "./path"; // single module
import Module from "./path"; // default export

import Module, { module } from "./path"; // both

Example 3: javascript include a js file from another

// jQuery
$.getScript('/path/to/imported/script.js', function()
{
    // script is now loaded and executed.
    // put your dependent JS here.
});

Example 4: javascript import

//add this to your package.json
//and make sure you are on node version 13.8.0 or above
"type": "module",
import { module } from "./path"; // then you are good to go to use import

Tags:

Html Example