javascript es6 export and import

Introducing Javascript Statements Export and Import

When writing modern web applications that’s include a lot of javascript it’s important to separate the code into modules. For this to work javascript provided us with two statements Export and Import.

 

 

If you are familiar with languages such as C# or Java or PHP may be you see the concept of namespaces which is used to separate the code into packages to make the code more modular, likewise in javascript there are also two statements that can be used in such situations which are Export and Import statements. Export statement used to export a function, a class, or variables from the current module. Import statement used to import exported functions, classes, or variables from other modules.

At the time of this writing not all browsers fully supports these two keywords but you can use something like webpack bundler to build such scripts that contain import and export. Most popular javascript frameworks such as angular, Reactjs and Vuejs relay on those two statements.

 

Export statement

The export statement used to export functions, classes, objects or variables from the current module so that it can be used by other modules by importing them.

Syntax

There are three types of export:

  • Named exports: expressions exported by their name and this type allows more than one export per module.
  • Default exports: expressions exported with the default keyword and this type allows only one export per module.

Named exports

In the above code i exported a list of data on one line for example export let var2 or export function myFunc(). Also you can export individual items when you declare items like this:

You can also rename named exports to avoid naming conflicts:

example:

module.js

index.js

When running those scripts you have to use a module bundler like webpack to build those scripts or if intend to run it directly in browsers you have to do the following:

  • You need to include this script in your HTML with a <script> element of type=”module“, so that it gets recognised as a module and dealt with appropriately.
  • You have to run the scripts using an Http Server.
  • In firefox you have to set to true the variable dom.moduleScripts.enabled in about:config

 

Default exports

In default exports you are allowed only to export one object, class or function for that default exports used for single file modules an example for this is Vuejs file based components.

module.js

entry.js

Note that when using default export you can use any name when importing the data like above import m from ‘./module’.

 

Import statement

The import statement used to import functions, classes, objects or variables from modules, usually these modules have to export data first so it can be imported.

Syntax

The import statement has a lot of variants, where you can import a default exported module, you can import named modules by enclosing them with parenthesis {export1, export2, …, exportN}, you can import and give an alias to the imported object so that you can refer to it with that alias.

Examples

Importing single named exports

Importing multiple named objects

Importing all named exports using *

You can import an entire module contents without mentioning the name of each exported item using the “*” like this:

Now to use any item of module.js you have to reference it with myModule like this:

Importing an export using alias

To avoid naming conflicts between import data, you can an alias like this:

Importing multiple exports and using alias

Importing a default export

When importing a default export you can use any name to reference the exported data like this example:

my-module.js

 

 

Share this: