Labs ICT
โญ Pro Login

Modules

1 min read | JavaScript Tutorial
โญ

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

ES6 Modules

Modules let you split code into separate files. Use export to expose values and import to consume them.

// math.js
export const PI = 3.14159;
export function add(a, b) { return a + b; }

// main.js
import { PI, add } from "./math.js";
console.log(PI);
console.log(add(2, 3));

Default Exports

Each module can have one default export. Import it without curly braces โ€” you can name it anything.

// greeter.js
export default function greet(name) {
  return "Hello, " + name;
}

// main.js
import greet from "./greeter.js";
console.log(greet("Alice"));

// You can also export a class as default
// export default class User { ... }

Named vs Default Imports

Mix named and default imports in one statement. Use * as to import everything as a namespace.

// utils.js
export const VERSION = "1.0";
export function log(msg) { console.log("[LOG]", msg); }
export default function error(msg) { console.error("[ERROR]", msg); }

// main.js
import error, { VERSION, log } from "./utils.js";
log("App version " + VERSION);
error("Something broke");

// Or import all
import * as utils from "./utils.js";
console.log(utils.VERSION);

๐Ÿงช Quick Quiz

What keyword is used to bring in exported values from another module?