DEV Community

Mahin Tazuar
Mahin Tazuar

Posted on

Javascript Modules

Module is just Javascript Es6 Feature. Inside a javascript file any script like variable, function, array, object can give access outside of the current file. This every thins is each one module. We need to use import and export to get access to this current file.
which module we want to give access to outside of the current file in this module we need to use the export keyword. Which file inside need to access this module we need to use import keyword. please check the below code you can realize how is that.

// play1.js
export let x = 'my name';
export let y = 'my hope';

// play2.js
import {x,y} from 'play1.js';
// import {x as V,y} from 'play1.js'; we can change the accesss //variable but same value we will get.
// import * everythins from './play1.js'; // we can access all //data using *

console.log(x); 

// default export
// play1.js
export let x = 'my name';
let y = 'my hope';
default y;

// play2.js
import DefaultEx ,{y} from 'play1.js';

console.log(x); 

// the condition is before using module ensures
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxtzzygd4f23kje0bvz3.jpeg) the same file is
//linked. If it using the vanilla js needs to use package.js
 with module enable code. 
Enter fullscreen mode Exit fullscreen mode

If we want to use default export we need to use the default keyword but it's not using the same line where it was defined. It needs to use after defining the code like abode the code example.
after default export, we can define the access name inside the import. And by default value store here. we do not need to call a specific name. We can get access to all things using *.

Top comments (0)