ES Modules: Import & Export
Learn how to split your JavaScript into organized, reusable files using the modern module system.
Why modules exist
In the early days of the web, all JavaScript went into one giant file. As websites grew, this became a nightmare — thousands of lines of code, naming collisions (two functions with the same name overwriting each other), and no clear way to organize things. Modules solve this by letting you split code into separate files, each with its own scope. A module only exposes what it explicitly exports, and other files only get what they explicitly import.
This is how every large JavaScript project is organized today. Google, YouTube, Amazon — they all use modules to manage millions of lines of code split across thousands of files.
ES Modules (ESM) were introduced in ES2015 (ES6) and are now the official standard module system for JavaScript. Browsers support them natively with `<script type="module">`. Older systems like CommonJS (`require()`) are still used in Node.js but are not the browser standard.