Exporting & Importing modules in javascript

Writing Moduler code in javascript by splitting the code in multiple files.

The two types of export i.e default way and named exports way:

lets understand them one by one:

1- Lets understand the default exports from below example:

// author.js file below

const author ={

name: 'Shiv'

};

export default person;

In order to import the above file as its a default export, we can import it with any name because whenever we will import this file it is always going to refer the default export, as shown below:

import author from './author.js' // the default export

import authorData from './author.js' // the default export

2- Now lets understand the named exports way, here we will need to provide exact names in curly braces so that it can target specific name on the exported file as shown in below example:

// sample.js file below

export const variable1 = false;

export const variable2= true;

import {variable1} from './sample.js' // the named export

import {variable2} from './sample.js' // the named export

Also there are two other ways to import the same as shown below:

1- import {variable2 as vb2} from './sample.js' // using alias we can give it any name as per our requirement by adding '**as' keyword followed by the name.

2- import * as sample from sample.js ;// if there are multiple named exports then we can do like this where star character can import everything & assigned it to sample & then sample will be holding javascript object with all the named exports as properties.

Thanks, Happy coding...