Lesson 20. Creating modules

 

After reading lesson 20, you will

  • Understand what a module is
  • Know how JavaScript behaves in modules
  • Create modules and export values

Before you learn how to create and use ES2015 modules, let’s define what a module in JavaScript is. At its most basic, a module is a JavaScript file that has its own scope and rules, and may import or export values to be used by other modules. A module isn’t an object: it has no data type, and you can’t store one in a variable. It’s simply a tool to break apart, encapsulate, and organize your code.

Modules are a great way to separate your logic into cohesive units and share logic across files without the cumbersome use of global variables. They also allow you to import only what’s needed, keeping the cognitive load down and making maintenance easier.

Consider this

Imagine you’re writing a large application. Would you use a single file for the entirety of the code or break it up into several smaller files? How would you make the various components of your application communicate and share resources across files?

20.1. Module rules

20.2. Creating modules

20.3. When does a JavaScript file become a module?

Summary