Javascript Modules es6 - Informatic Point

Javascript Modules es6

Javascript Modules es6. A module is a script file that performs functionality that becomes loaded into the main script using export and import directives. Module encapsulates the functionality and presents it to other JavaScript files as libraries. The export keyword is used to label variables, functions, etc. that necessary to be accessible outside that module, and the Javascript keyword import provides functionality to be imported toward other modules for use.

Therefore, it will be good to say that each module contain of three sections:

  • Imports.
  • Codes.
  • Exports.

When we perform a module, they have strict mode permitted by default, and their performance is deferred i.e. the script file(module) is executed only after the HTML document is parsed. During writing a script element, we can determine the browser to operate the script element as a module by specifying the value “module” to the type attribute.

<script type="module" src="script.mjs"></script>

It is vital to note that modules are assessed once, even if they are imported into various other modules. While scripts are assessed at any time as we add them to Document Object Model (DOM). The browser knows it is a module because of the type attribute on the script element. In the above example, the file extension is .mjs, to assure that whoever inspects the file gets it is a module and not a script because modules are individually handled as opposed to “classic” script. 

Examples to Implement JavaScript Modules es6:)

<script type="module">
import {scriptmessage} from '/script.js';
document.body.innerHTML = scriptmessage('Adam');
</script>

script.js🙂

export function greetings(user) {
return 'Hello World, ${user}!';
}

Output: “Hello World”

The distinct ways of executing JavaScript source code are listed below, which assists us in knowing how JavaScript modules are loaded on browsers and servers and how they have developed over the years.

M-ModuleRuns onLoadedFilename 
Scriptbrowsersasync.js
CommonJS  Mserversasync.cjs .js
ECMAScript Mbrowsers/Serverasync.mjs.js
AMD Mbrowsersasync.js

We can understand that in our example we have used .mjs extension for showing that those script files are a module, though not necessary but helpful in showing that these files are modules of the application.

The most important points to know about modules are:

  • The web browsers load and run the script modules automatically.
  • Modules exchange functionalities through the import/export method.
  • Modules by default have used strict
  • Modules are performed once.
  • Module execution is postponed.
  • Modules have their own, local top-level scope.

Conclusion

In this article, we knew what Javascript Modules es6 is and how it is performed and the advantages it leads to the table and how it is modified from our normal scripts. Modules split the code into sections with clear interfaces and dependencies. The interface is the obvious part that other modules can have a look at, and the dependencies are the piece that other modules use.

Leave a Reply

Your email address will not be published. Required fields are marked *