JavaScript Modules
Before ES6 (2015), JavaScript had no native module system. Developers had to use multiple <script> tags or rely on external libraries. Today, ES Modules (ESM) allow us to export and import code seamlessly.
Named Exports
You can export multiple variables or functions from a file using named exports. When you import them, you must use their exact names wrapped in curly braces {}.
Default Exports
A file can have only ONE default export. This is useful when a module is primarily designed to do one specific thing (like a React Component). When importing a default export, you do not use curly braces, and you can name the imported variable whatever you want.
Aliases
If you import two functions with the same name from different modules, your code will break. To fix this, use the as keyword to rename them upon import.
View Full Transcript+
In HTML, to load a JavaScript module, you must add type="module" to the script tag. Modules are deferred by default, meaning they load in parallel and execute after the HTML document is parsed. Furthermore, modules strictly operate in "strict mode" automatically. This prevents the usage of undeclared variables and helps write cleaner code.
