JavaScript Module Import

Back to JS page

Table of Contents

Named Imports

Named imports use curly braces {} and must match the exported name exactly. You can import multiple items:

utils.js - Module file:

export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
export const multiply = (a, b) => a * b;
export const divide = (a, b) => a / b;
export const PI = 3.14159;

Main HTML file:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Module Import</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Named Imports</h4>
<p id="demo"></p>

<script type="module">
import { add, multiply, PI } from './utils.js';

document.getElementById("demo").innerHTML =
  "add(10, 5) = " + add(10, 5) + "<br>" +
  "multiply(10, 5) = " + multiply(10, 5) + "<br>" +
  "PI = " + PI;
</script>

</body>
</html>

Example 1

Result View Example

Default Import

Default imports don't need curly braces. You can name the import anything you want:

greet.js - Module file:

export default function(name) {
  return "Hello " + name + "!";
}

Main HTML file:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Module Import</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Default Import</h4>
<p id="demo"></p>

<script type="module">
import sayHello from './greet.js';

document.getElementById("demo").innerHTML =
  sayHello("John") + "<br>" +
  sayHello("Jane");
</script>

</body>
</html>

Example 2

Result View Example

Default + Named Import

You can import both default and named exports in a single statement:

calc.js - Module file:

export default function(a, b) {
  return a + b;
}

export const square = (x) => x * x;
export const cube = (x) => x * x * x;

Main HTML file:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Module Import</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Default + Named Import</h4>
<p id="demo"></p>

<script type="module">
import myAdd, { square, cube } from './calc.js';

document.getElementById("demo").innerHTML =
  "Default (add): 10 + 5 = " + myAdd(10, 5) + "<br>" +
  "square(5) = " + square(5) + "<br>" +
  "cube(3) = " + cube(3);
</script>

</body>
</html>

Example 3

Result View Example

Importing All

Use * to import everything from a module as a namespace object:

colors.js - Module file:

export const red = "#FF0000";
export const green = "#00FF00";
export const blue = "#0000FF";
export const white = "#FFFFFF";
export const black = "#000000";

Main HTML file:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Module Import</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Importing All</h4>
<p id="demo"></p>

<script type="module">
import * as Colors from './colors.js';

document.getElementById("demo").innerHTML =
  "Red: " + Colors.red + "<br>" +
  "Green: " + Colors.green + "<br>" +
  "Blue: " + Colors.blue + "<br>" +
  "White: " + Colors.white + "<br>" +
  "Black: " + Colors.black;
</script>

</body>
</html>

Example 4

Result View Example

Document

Document in project

You can Download PDF file.

Reference