Modules

Modules are the primary construct by which we organize our JavaScript code.

At first glance, a JavaScript module is just a JavaScript file. It arises naturally when splitting large JavaScript programs into several sections, each stored in a separate file.

Additionally, modules create scope. The variables declared in a module have "file scope." Thus, they are not accessible outside the module. A module system provides a mechanism to export any value declared in a module and import them in another module when needed. Therefore, modules are another way (in addition to Class and Closure) to create encapsulation.

For a long time, JavaScript modules were implemented via third-party libraries (not built into the language). ECMAScript 2015 (ES6) was the first time that JavaScript provided built-in modules. We will focus on ES6 modules.1

1

Later, we need to learn an older module system, CommonJS, which is used by NodeJS.