InterviewHubPRO
Section Overview
Q33

Question 33 of 521

Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

6%
JavaScript Internals (521 Qs)•Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

Why do you need modules

Below are the list of benefits using modules in javascript ecosystem

  1. Maintainability
  2. Reusability
  3. Namespacing

Practical Code Example

javascript
// Without modules: Global namespace collisions
// file1.js: var user = 'Alice';
// file2.js: var user = 'Bob'; (overwrites file1!)

// With ES Modules: Encapsulated private scope
// userService.js
const privateSecret = 'secret_key_123'; // Not visible outside
export function getUser(id) {
  return { id, auth: privateSecret };
}