Skip to content

Instantly share code, notes, and snippets.

@Oluwasetemi
Created November 7, 2025 18:41
Show Gist options
  • Select an option

  • Save Oluwasetemi/dc18dfee49cb08c121aea8a04801c233 to your computer and use it in GitHub Desktop.

Select an option

Save Oluwasetemi/dc18dfee49cb08c121aea8a04801c233 to your computer and use it in GitHub Desktop.
teaching modules to my student. To run this code becareful with the comments
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Learning ES Modules</title>
</head>
<body>
<h1>Learning ES Modules</h1>
<script type="module" src="index.js"></script>
</body>
</html>
// import {sum} from './sum.js';
// import sum from './sum.js';
// renamed import
// import * as math from './math.js';
// import { PI, sum as add, subtract as sub, Person } from './math.js';
// top level await
// const math = await import('./math.js');
import('./math.js').then(module => {
console.log(module)
const math = module;
console.log(math.PI)
}).catch(err => console.log(err))
// console.log(math.PI)
// module.exports = sum; // no destructuring (default export)
const math = require('./math.js')
// exports.sum = sum; // destructuring (named export)
const { PI, sum, subtract, Person } = require('./math.js')
// console.log(PI)
// console.log(add(10, 1))
// console.log(sub(10, 1))
// const tutor = new Person('Setemi', 99);
// tutor.sayHello();
// import * as say from './say.js'
// import { sayHi as hi, sayBye as bye } from './say.js'
// say.sayHi('John')
// say.sayBye('John')
// const module = await import('./path/to/module.js')
// ES Modules
// export function sum(a, b) {
// return a + b;
// }
export const PI = 3.14;
export class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
export function sum(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
// export { PI as pi, sum as add, subtract as sub, Person as Tutor }
// there must only be one default export
export default sum;
console.log(sum(1, 2));
// requireJS | CommonJS
// module.exports = sum; // no destructuring (default export)
// exports.sum = sum; // destructuring (named export)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment