Es6 - Javascript Named and Default Modules Basic Guide with examples

In this blog post,  We are going to learn about named and default modules in Javascript or ES6 Version with examples.

Es6 Modules 

Javascript has no support modules in previous versions of ES6. Es6 introduced modules to organize and reuse code in various parts of applications Es6 modules are basically a javascript code which contains variables, functions which will be declared in the javascript file. These can not be used outside unless the module is declared to exported use. This is very simple and powerful functionality used by Nodejs and client-side frameworks.

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 the same module

There are two types of modules can be defined in ECMAScript 2015

  • Default Module-Default modules are  modules which contain only one per module
  • Named Modules -The modules contain multiple modules in a file

Import and Export Default Module

 In 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 export keyword

Once the module is exported, It is ready to use in other modules using import keyword. Here is a syntax for import keyword

import ModuleName from 'filepath without extension'

Similar Posts