JavaScript JS Modules Interview Questions and Answers

Prepare for questions about ES module imports, exports, module scope, and common module-system differences.

Frequently Asked Questions

Important questions are highlighted in the sections below; select one to jump to its answer.

Theoretical JavaScript Interview Questions

A module is an independent, reusable piece of code that encapsulates related functionality in its own file scope. ES6 modules use the import and export keywords to share functionality between files, enabling better code organization, avoiding global scope pollution, and explicit dependency management.

Export items using the export keyword: export const name = ... or export default myFunction. Import them with import: import { name } from './module.js' for named exports, or import myFunction from './module.js' for default exports. Use import * as module from './module.js' for all exports.

CommonJS uses require() and module.exports, and it became popular in Node.js. ES Modules use import and export, and they are the standard module system in modern JavaScript for both browsers and Node.js. ES Modules are easier for tools to analyze, which helps with optimizations like tree shaking.

Polyfilling is adding JavaScript code that emulates newer features in older browsers that don't support them natively. A polyfill detects if a feature is missing and provides a fallback implementation, ensuring cross-browser compatibility without requiring users to update their browsers.

Transpiling converts modern JavaScript code (e.g. ES6+) into an equivalent older version (e.g. ES5) that is compatible with older browsers. Tools like Babel handle this automatically. Transpiling converts syntax like arrow functions and classes, while polyfilling adds missing APIs.

Polyfilling adds new API functionality to older environments by implementing missing features in JavaScript code (e.g. adding Array.from to older browsers). Transpiling converts modern syntax to equivalent older syntax (e.g. arrow functions to regular functions). Polyfilling is about features; transpiling is about syntax.

A module is a file or unit with its own scope that explicitly imports and exports values. Static ES module imports and exports are resolved as part of module processing.

A named export exposes a value under an explicit name, which consumers import using that name. A module can provide multiple named exports.

A module may provide one default export, imported without braces and under a consumer-chosen local name. Named exports can also coexist with it.

Top-level module declarations are scoped to that module rather than added as global object properties. Modules are strict mode and have their own top-level this behavior.

Yes. ES module code always runs in strict mode, so some mistakes tolerated in non-strict scripts instead cause errors or different behavior.

An import reflects the current exported binding rather than a copied value. Importers can read the binding but cannot assign to it.

In a cycle, a module may access an imported binding before its exporter has initialized it, depending on evaluation order. Minimize cycles and avoid premature reads during evaluation.

import() loads a module on demand and returns a promise for its namespace object. It supports conditional features and code splitting; handle rejection if loading can fail.

A module loader generally evaluates a given module identity once and reuses its instance on later imports. Identity depends on resolution and the host module system.

A module can suspend evaluation while awaiting a promise, and dependent modules wait for it to complete. This simplifies asynchronous initialization but can delay dependent modules.