JavaScript Interview Question #96

What are ES modules in JavaScript?

Promises, Async/Await, Fetch & Modern JavaScript Mid-Level Intermediate

Quick Interview Answer

ES modules are the standardized JavaScript module system based on export and import syntax. Modules let applications split code into reusable files with explicit dependencies.

Detailed Explanation

ES modules are the standardized JavaScript module system based on export and import syntax. Modules let applications split code into reusable files with explicit dependencies.

In browsers, module scripts use type="module". Modules have their own scope, use strict mode semantics automatically, and support features such as static imports and dynamic import(). Modern build tools and Node.js can also work with ES modules.

Code Example

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

// app.js
import { add } from "./math.js";

console.log(add(2, 3));