CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ“¦ Understanding ES6 Modules in JavaScript

ES6 modules provide a native way to organize and share JavaScript code using import and export. Standard for modern JavaScript development and code organization.

JavaScript applications have grown massively in size and complexity over the years. To manage this growth, developers need tools for organizing code, avoiding global scope pollution, and promoting reusability. Enter the ES6 Module System β€” a native solution to modular programming in JavaScript. This article explores what ES6 modules are, how they work, and why they're now the standard for modern JavaScript development. -- πŸ” What Are ES6 Modules? An ES6 Module is a JavaScript file that explicitly exports variables, functions, or classes so they can be imported into other files. They were introduced in ECMAScript 2015 (ES6) and are now supported in all modern browsers and Node.js environments. -- 🎯 Why Use Modules? Encapsulation: Avoid polluting the global namespace. Reusability: Share logic across files. Maintainability: Organize code by responsibility. Dependency Management: Declare what’s used where. Performance: Modules are statically analyzable (great for bundlers and tree-shaking). -- πŸ“€ Exporting from a Module There are two types of exports: and . 1. Named Exports You can export multiple items from the same module: 2. Default Export Each module can have one export: You can also export a value directly: -- πŸ“₯ Importing in Other Files 1. Import Named Exports You can also rename imports: 2. Import Default Export 3. Import All (Namespace Import) -- πŸ§ͺ Combining Named and Default Exports -- βš™οΈ Module Characteristics Behavior Variables stay within the module, not global Strict mode by default Module is loaded and run once (shared instance) Static structure / must be at the top level of a file Feature CommonJS () Syntax / Execution Dynamic Top-level only No Tree-shaking ❌ Not supported Standard ❌ Node-specific Mistake Forgetting in Move to top-level scope Using extension inconsistently Use only one format per project (or use interop carefully) | -- πŸ”š Conclusion The ES6 module system is now the standard way to organize JavaScript code, replacing older patterns like IIFEs, CommonJS, and AMD. It improves code clarity, enforces modular structure, and provides powerful tooling benefits (like tree-shaking and scope isolation). If you're building modern JavaScript applicationsβ€”whether in the browser or Node.jsβ€”you should embrace ES6 modules as your go-to solution for scalable code architecture. -- <!-quiz-start --Q1: How many default exports can a module have? [ ] Unlimited [ ] Two [x] One [ ] None, only named exports are allowed Q2: What is the correct way to import a default export? [ ] [x] [ ] [ ] Q3: Which statement about ES6 modules is TRUE? [ ] Modules run in non-strict mode by default [ ] statements can be placed anywhere in the file [x] Module scripts have behavior implicitly enabled in browsers [ ] ES6 modules support dynamic syntax <!-quiz-end --
JavaScriptCore Concepts
πŸ›‘ AbortController: Canceling Async Operations in JavaScript
medium
πŸ”’ Closures in JavaScript β€” The Complete Guide
hard
πŸ“¦ Understanding ES6 Modules in JavaScript
medium
⚑ JavaScript Event Loop: Complete Guide to Asynchronous Execution
hard
🧭 Arrow Functions vs Function Declarations in JavaScript
easy
πŸ—‘οΈ Garbage Collection in JavaScript β€” Memory Management & Leak Prevention
hard
πŸ—οΈ Constructor Functions in JavaScript
medium
πŸ‘οΈ MutationObserver: Watching DOM Changes in JavaScript
medium
πŸ” Understanding `of` in JavaScript – `for...of` Loop Deep Dive
hard
πŸ”— Prototype and Prototype Inheritance in JavaScript
medium
πŸ•΅οΈ What Are Proxies in JavaScript? (With Practical Use Cases)
medium
🎯 Scope in JavaScript β€” The Complete Guide
hard
πŸ”„ Script Loading: async vs defer vs Both
hard
πŸ“€ JavaScript Spread Operator (...) Explained
easy
🎯 The JavaScript `this` Keyword: Complete Guide to Context Binding
medium
3 of 15
LibraryJavaScriptCore Concepts3 of 61

πŸ“¦ Understanding ES6 Modules in JavaScript

jsgeneral-conceptsmedium

JavaScript applications have grown massively in size and complexity over the years. To manage this growth, developers need tools for organizing code, avoiding global scope pollution, and promoting reusability. Enter the ES6 Module System β€” a native solution to modular programming in JavaScript.

This article explores what ES6 modules are, how they work, and why they're now the standard for modern JavaScript development.


πŸ” What Are ES6 Modules?

An ES6 Module is a JavaScript file that explicitly exports variables, functions, or classes so they can be imported into other files.

They were introduced in ECMAScript 2015 (ES6) and are now supported in all modern browsers and Node.js environments.


🎯 Why Use Modules?

  • Encapsulation: Avoid polluting the global namespace.
  • Reusability: Share logic across files.
  • Maintainability: Organize code by responsibility.
  • Dependency Management: Declare what’s used where.
  • Performance: Modules are statically analyzable (great for bundlers and tree-shaking).

πŸ“€ Exporting from a Module

There are two types of exports: named and default.

1. Named Exports

You can export multiple items from the same module:

// utils.js
export const PI = 3.14;
export function add(a, b) {
  return a + b;
}
export class Circle {
  constructor(radius) {
    this.radius = radius;
  }
}

2. Default Export

Each module can have one default export:

// logger.js
export default function log(msg) {
  console.log(msg);
}

You can also export a value directly:

export default 42;

πŸ“₯ Importing in Other Files

1. Import Named Exports

import { PI, add } from './utils.js';
console.log(add(2, 3)); // 5

You can also rename imports:

import { add as sum } from './utils.js';

2. Import Default Export

import log from './logger.js';
log('Hello world');

3. Import All (Namespace Import)

import * as Utils from './utils.js';
console.log(Utils.PI);

πŸ§ͺ Combining Named and Default Exports

// math.js
export const multiply = (a, b) => a * b;
export default function divide(a, b) {
  return a / b;
}
// main.js
import divide, { multiply } from './math.js';

βš™οΈ Module Characteristics

FeatureBehavior
File-scopedVariables stay within the module, not global
Strict mode by defaultNo need for "use strict"
Singleton executionModule is loaded and run once (shared instance)
Static structureImports/exports are statically analyzed
Top-level onlyimport/export must be at the top level of a file

🌐 Using Modules in Browsers

Use the type="module" attribute in <script>:

<script type="module" src="main.js"></script>

Notes:

  • Modules run in strict mode.
  • defer is implicitly enabled, so the script loads after the HTML is parsed.
  • Module scripts are scoped, so their top-level declarations are not global.

πŸš€ Using Modules in Node.js

Node.js added support for ES6 modules via:

  • Files with .mjs extension, or
  • package.json with "type": "module"

Example:

{
  "type": "module"
}
// math.mjs
export function square(x) {
  return x * x;
}
// main.mjs
import { square } from './math.mjs';

πŸ“¦ ES6 Modules vs CommonJS (Node.js require())

FeatureES6 Modules (import/export)CommonJS (require/module.exports)
Syntaximport / exportrequire() / module.exports
ExecutionStaticDynamic
Top-level onlyYesNo
Tree-shakingβœ… Supported❌ Not supported
Standardβœ… Official ECMAScript❌ Node-specific

🧱 Real-World Example: Building a Module-Based App

Directory Structure:

project/
+-- index.html
+-- main.js
+-- utils/
|   +-- math.js

math.js

export function add(a, b) {
  return a + b;
}
export function subtract(a, b) {
  return a - b;
}

main.js

import { add, subtract } from './utils/math.js';

console.log(add(10, 5));       // 15
console.log(subtract(10, 5));  // 5

index.html

<script type="module" src="main.js"></script>

πŸ›‘ Common Mistakes to Avoid

MistakeFix
Forgetting type="module" in <script>Add type="module"
Using import inside functions or conditionalsMove to top-level scope
Using .js extension inconsistentlyAlways include extension in browser environments
Mixing CommonJS and ES6 modulesUse only one format per project (or use interop carefully)

πŸ”š Conclusion

The ES6 module system is now the standard way to organize JavaScript code, replacing older patterns like IIFEs, CommonJS, and AMD. It improves code clarity, enforces modular structure, and provides powerful tooling benefits (like tree-shaking and scope isolation).

If you're building modern JavaScript applicationsβ€”whether in the browser or Node.jsβ€”you should embrace ES6 modules as your go-to solution for scalable code architecture.


Quick Quiz

Test your understanding with 3 quick questions

Q1How many default exports can a module have?
Q2What is the correct way to import a default export?
Q3Which statement about ES6 modules is TRUE?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna