Advantages of Modules:
- The code can be separated into multiple files and reusability is improved
- Modules can be reused in multiple other applications or modules
- Modules are like packages in java which we can avoid namespace issues
- All variables or functions defined in the module are private in the same module
Two types of modules can be defined in ECMAScript 2015
- Default Module-Default modules are modules that contain only one per module
- Named Modules -The modules contain multiple modules in a file
Import and Export Default Module
This single module is declared for export functionality. Modules need to be declared with an export keyword to make use of it in another part of the code. Import keyword is used to import the modules Here is a syntax for import and export Default module declaration.
export default ModuleName // Export is declared with the export keyword
Once the module is exported, It is ready to use in other modules using the import keyword. Here is a syntax for import keyword
import ModuleName from 'filepath without extension'.