Write code that is easy to delete, not easy to extend.

Modules

Gods that you write now, will be rewritten, if not for the purpose of refactoring and updating your codes then surely fixing your bug. In a system, we should really be careful on how we design our program, the more we make everything tangled together , the harder it is to refactor and decouple things our.

Codes should be written in a way that they do not depend on each other(lose coupling) so that they can be easy to delete and change.

If you write your codes in a way that they are corelated to each other too much, then you surely will have hard time understanding the codes you wrote. Creating a monolithic code will make your system hard to deal with.

Modules to the rescue

A module is a piece of program that specifies which other pieces it relies on and which functionality it provides for other modules to use (its interface).

Using this approach, a system is designed a way that only things that needs to be exposed to the outside world are exposed and other internal, implementation details are kept at away. When one piece of code from one module depends on another piece of code in another module, it’s called dependencies.

Writing codes in one module is not enough, you need to explicitly make it accessible for use in other modules by exporting the code.

In the early days of JavaScript, there was no such a thing as module, and yet people used to build scalable apps. they used to encapsulate codes into function and use IIFE to encapsulate codes, but nowadays , these techniques are obsolete.

ES6 MODULES VS COMMON JS

Common JS

The most used approach to encapsulate javascript codes is called the common js pattern, this approach uses the require keyword to mode dependencies around. It works by loading the module and returning it’s interface.

ES6 Module

ES6 came to fix certain flows the common js module patterns had, the concept of modules remain the same but, instead of calling a function to access it’s dependencies, you simply can import the piece of code and access it from your file.