Skip to content

Latest commit

 

History

History
41 lines (26 loc) · 1.23 KB

README.md

File metadata and controls

41 lines (26 loc) · 1.23 KB

41. ES modules

Split browser code into explicit dependencies.

Learn

  • type=module
  • import
  • export
  • module scope

How it works

A module has its own scope and can import or export bindings. The browser loads module dependencies by URL; serve examples over localhost because file URLs can have module loading restrictions. Modules run in strict mode and are deferred by default. Keep each module focused on one responsibility.

Common mistake

A script without type=module cannot use import syntax in the browser.

Guided challenge

Split a formatter and a page entry point into two files and run them through a local server.

Run the example

Open example.html in a browser and edit example.js. Save, refresh, and use the Console and debugger to inspect values.

Completion check

  • Explain the core idea in your own words without reading the code.
  • Change one input and predict the output before running it.
  • Test one boundary or failure case, then record what you changed.

Local server

Run python3 -m http.server 8000 from the JavaScripts folder, then open this lesson through http://localhost:8000/. Fetch, modules, storage, history, and workers can behave differently or fail under file URLs.