Introduction to JavaScript Modular programming

Nerd, Solutions 

Modular programming is the technique that allow code to be broken in modules, which provides certain functionality or perform certain tasks into the program. This technique is one of the widely used programming technique in software design. Previous versions of ECMAscript 6 do to support this programming technique until ECMAscript 6, which support modular programming natively.


Advantages of using modular programming technique

Implementing ECMAscript modular programming

Because ECMAscript 6 support modular programming natively, we can implement it very easily without unnecessary hacks. In JavaScript, every module needs to have its own file with the .js extension. All codes related to the module needs to be included in the same file and any valid JavaScript is allowed to be used in the module file.


Exporting JavaScript Modules

In order for the modules to be used we need to export them first. JavaScript module file can export any number of variables, functions, classes or any other entities. The exportation is achieved by the export statement. There are number of different format for export statement, which are:

 Rules that guide the export statement


Importing module

Before using our modules, we need to import them first into our main code. This can be achieved by using the import statement. Import statement has two part. the first part is the variable part - describe the name of imported variable - and the second part is the retrieved module path - this is the location of the imported module in the filesystem. The import statement comes in many different formats, which are:

These import formats can be combined to achieve a desired outcome.

Rules to guide the import statement