Imagine a kitchen with no cupboards, drawers or shelves. Every pot, spoon, plate, spice jar and tea towel is just piled up in one gigantic heap in the middle of the floor. Want a teaspoon? Good luck. Dig.
That's what it's like to have a whole program in one enormous file. It works when the program is small. But real apps can have hundreds of thousands of lines of code. In one file, you'd never find anything, two people couldn't work on it at the same time without tripping over each other, and a variable name used in one part might accidentally clash with the same name in another.
The solution is to split your code into separate files, called modules. Each module is like a labeled drawer: one for date helpers, one for the shopping cart, one for talking to the server. Each module:
- Has its own scope. Variables inside a module are private to it, unless you choose to share them.
- Exports the things it wants to share with other modules.
- Imports the things it needs from other modules.