Table of Contents
The export keyword is used to export functions, objects, or primitive values from a module file so they can be used in other files with the import keyword.
There are two types of exports: Named Exports and Default Exports.
Named exports allow you to export multiple items from a module. The import must use the exact same name:
math.js - Module file with named exports:
export const square = (x) => x * x;
export const cube = (x) => x * x * x;
export const double = (x) => x * 2;
Main HTML file:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Module Export</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Named Exports</h4>
<p id="demo"></p>
<script type="module">
import { square, cube, double } from './math.js';
document.getElementById("demo").innerHTML =
"square(5) = " + square(5) + "<br>" +
"cube(3) = " + cube(3) + "<br>" +
"double(10) = " + double(10);
</script>
</body>
</html>

Result View Example
A module can have one default export. Default exports don't need curly braces when importing, and can be imported with any name:
message.js - Module file with default export:
const greet = (name) => "Hello, " + name + "!";
export default greet;
Main HTML file:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Module Export</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Default Exports</h4>
<p id="demo"></p>
<script type="module">
import myGreeting from './message.js';
document.getElementById("demo").innerHTML =
myGreeting("John") + "<br>" +
myGreeting("Jane");
</script>
</body>
</html>

Result View Example
You can combine default and named exports in the same module:
toolbox.js - Module file with both default and named exports:
export default function(a, b) {
return a + b;
}
export const subtract = (a, b) => a - b;
export const multiply = (a, b) => a * b;
Main HTML file:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Module Export</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Combining Default + Named</h4>
<p id="demo"></p>
<script type="module">
import myAdd, { subtract, multiply } from './toolbox.js';
document.getElementById("demo").innerHTML =
"Default (add): 10 + 5 = " + myAdd(10, 5) + "<br>" +
"Named (subtract): 10 - 5 = " + subtract(10, 5) + "<br>" +
"Named (multiply): 10 * 5 = " + multiply(10, 5);
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.