A module is just a javascript file which can be later imported by other javascript files.
Syntax export
There are different ways to export a file.
//Export a function
export function print(msg) {
console.log(msg);
}
//Export a variable
export var myMsg = 'Hello ES6!!';
//Export a function without name
export default (function () { //some code })();
//Export a variable without name
export default 50;
> Note: In ES6 classes, let and const can also be exported. Also name of a function, class name or variable name are used as export name.
Syntax import
There are different ways to import as well.
//myFunz.js file
export function printName(msg) {
console.log('My name is ' + msg);
}
export var myDefaultName = 'Foo';
//App.js file
import {myDefaultName, printName as printMyName} from './myFunz';
printMyName('Bar'); //print My name is Bar
console.log(myDefaultName); //print Foo
Note: Import a module and change its name with
as
keyword.
Import a default module. Syntax import default
//myDefault.js file
export default function printName(msg) {
console.log(msg);
}
//App.js file
import printName from './myFunz';
printName('Default Module'); //print Default Module