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 ...

Get Get Programming with JavaScript Next 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.