Modules and Clean Coding in JavaScript

Modules and Clean Coding = how to split code into files and keep it readable.

This chapter has 2 lessons and 8 exercises you run and check in your browser. Plan on about 43 minutes.

After this chapter, you can:

  • Split a program into files that share only what they export.
  • Load code only when it is needed with import().
  • Let strict mode catch your mistakes.
  • Write code with clear names that others can read.

Every lesson has short explanations, code boxes you edit and run in the live editor, and a web page example you can click.

Before you start, finish DOM Manipulation.

Next chapter: Performance Optimization.

Lessons in this section

  1. Import and Export

    What a module is and why module scope matters, named and default exports, importing with as and import * as, type="module" and file paths, common module errors, dynamic import(), and CommonJS.

    • Explain what a module is and how module scope differs from global scope
    • Write named exports, a default export and an export list
    • Import named and default exports, rename with as, and import everything with import * as
    • Load modules with type="module" and correct ./file.js paths, and fix the common module errors
  2. Clean Coding

    Strict mode and the silent errors it turns into real ones, then modern habits (const and let, ===, no implicit globals, rest parameters) and naming conventions for clean, maintainable code.

    • Turn on strict mode and know where the directive must go
    • Explain how strict mode turns silent mistakes into errors
    • Describe strict-mode behaviour for undeclared variables, this and duplicate parameters
    • Choose const, let and === over var and ==