In fact, modules have many advantages. The most important ones, in my opinion, are the following:
Maintainability: By definition, a module is self-sufficient. A well-designed module is intended to reduce the dependencies of parts of your codebase as much as possible, so that it can grow and improve independently of each other. It is much easier to update one module when it is separated from other parts of the code.Going back to our book, for example, if you want to make a small change to one chapter and it causes changes to some other section of your book, it will be a nightmare. So a chapter should be written so that when you make edits, you don’t have to affect other chapters.
Namespacing: In JavaScript, variables that are outside of top-level functions are considered global (anyone can access them). This is why it is very common to have “namespace pollution”, where completely unrelated code links global variables.Sharing global variables in code that is not related to each other is very bad in development.Later in this article we will see that modules allow us to avoid global namespace pollution by creating private spaces for our variables.
Reusability: Let’s be honest. All of us have copied code into new projects that we have written before. For example, let’s say you copied some helper methods from a previous project into a new project.Ok, but if you find the best way to write this part, you will have to remember all the places where this code appeared to update it.This is definitely a huge waste of time. It would be much easier to write a module and reuse it over and over again.