codegurukool.com · Free coding tutorials & online compiler

Home › JavaScript Tutorial › JS modules

JS modules

Split code into files.

Mark a script with type='module', then export from one file and import in another. Modules are strict, deferred and scoped. This demo runs a module inline.

Example code

<p id='o'></p>
<script type='module'>
// math.js:  export const add=(a,b)=>a+b;
// main.js:  import {add} from './math.js';
const add=(a,b)=>a+b;
document.getElementById('o').textContent='Module ran, 2+3='+add(2,3);
</script>

Run this code in the online editor

← JS asynchronousJS meta and proxy →