AceDevHub
Beginner JavaScript Interview QuestionsBeginnerConcept

JavaScript · Question 26

What are JavaScript modules, and how do import and export work?

Direct answer

JavaScript modules split code into files with explicit exports and imports, giving each module its own scope and a dependency graph managed by the runtime or tooling.

An ES module can expose values with export and consume them with import. For example, export const add = (a,b) => a+b; can be consumed using import { add } from './math.js';.

  • Named exports allow multiple explicitly named values.
  • A default export provides one default binding per module, imported with a locally chosen name.
  • Module imports are statically analyzable, which helps tools understand dependencies.
  • Modules have their own top-level scope rather than sharing ordinary script-level declarations.