🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Import & Export

AI & DATA SCIENCE // import-export

ES6 modules use export to expose values and import to consume them. Modules have their own scope — no global pollution.

Syntax

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

// main.js
import { PI, add } from './math.js';

Deep Dive Course

**ES modules** provide a clean way to split code across files. Use **named exports** for multiple values, **default export** for the primary value. Modules are evaluated once and cached — the same module object is shared. In browsers, add `type="module"` to script tags. Node.js uses `.mjs` or `type: module` in package.json.

1Understanding Import & Export

ES modules provide a clean way to split code across files. Use named exports for multiple values, default export for the primary value. Modules are evaluated once and cached — the same module object is shared. In browsers, add type="module" to script tags. Node.js uses .mjs or type: module in package.json.

💡

Modules are loaded asynchronously and run in strict mode automatically.

editor.html
// utils.js
export const VERSION = '2.0.0';

export function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

export class Logger {
  log(msg) { console.log(`[${new Date().toISOString()}] ${msg}`); }
}
localhost:3000

2Practical Example

Here is a real-world application of Import & Export showing how it is used in production JavaScript code.

editor.html
// app.js
import { VERSION, capitalize, Logger } from './utils.js';
import { add as sum } from './math.js'; // rename on import
import * as utils from './utils.js';    // import all as namespace

const logger = new Logger();
logger.log(`App v${VERSION} starting...`);
console.log(capitalize('hello')); // Hello
localhost:3000

3Best Practices

Follow these guidelines when working with Import & Export:

1. Use named exports for libraries (treeshakeable)

2. Use default export for the main thing a module does

3. Keep one concern per module

⚠️

Tip: Modules are loaded asynchronously and run in strict mode automatically.

editor.html
// utils.js
export const VERSION = '2.0.0';

export function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

export class Logger {
  log(msg) { console.log(`[${new Date().toISOString()}] ${msg}`); }
}
localhost:3000

Examples

Example 01Basic Usage
// utils.js
export const VERSION = '2.0.0';

export function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

export class Logger {
  log(msg) { console.log(`[${new Date().toISOString()}] ${msg}`); }
}
Example 02Advanced Example
// app.js
import { VERSION, capitalize, Logger } from './utils.js';
import { add as sum } from './math.js'; // rename on import
import * as utils from './utils.js';    // import all as namespace

const logger = new Logger();
logger.log(`App v${VERSION} starting...`);
console.log(capitalize('hello')); // Hello

Best Practices

  • Use named exports for libraries (treeshakeable)
  • Use default export for the main thing a module does
  • Keep one concern per module

Interview Question

What is the difference between named exports and default exports?

Hint: Multiple vs one, curly braces vs no curly braces.

Named exports use 'export const x' and are imported with curly braces: 'import { x } from'. A module can have many named exports. Default export uses 'export default value' and is imported without braces: 'import value from'. A module can have only ONE default export. Named exports are better for tree-shaking (bundlers can remove unused ones).

Exercises

MediumPractice using Import & Export in a real scenario.
View Solution
// utils.js
export const VERSION = '2.0.0';

export function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

export class Logger {
  log(msg) { console.log(`[${new Date().toISOString()}] ${msg}`); }
}

Frequently Asked Questions

What is the difference between named exports and default exports?

Named exports use 'export const x' and are imported with curly braces: 'import { x } from'. A module can have many named exports. Default export uses 'export default value' and is imported without braces: 'import value from'. A module can have only ONE default export. Named exports are better for tree-shaking (bundlers can remove unused ones).

Related Functions

Export-by-DefaultExport-NamedModules