JavaScript modules 101

The practice of breaking down programs and libraries into modules is called modular programming.

In JavaScript, a module is a collection of related objects, functions, and other components of a program or library that are wrapped together and isolated from the scope of the rest of the program or library.

A module exports some variables to the outside program to let it access the components wrapped by the module. To use a module, a program needs to import the module and the variables exported by the module.

A module can also be split into further modules called sub-modules, thus creating a module hierarchy.

Modular programming has many benefits. Some benefits are as follows:

  • It keeps our code both cleanly separated ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.