Modules
A module is just a javascript file really, one that can be called by anouther javascript program.
Modules can load each other and use special directives export and import to interchange functionality, call functions of one module from another one.
export
keyword labels variables and functions that should be accessible from outside the current module.
import
allows the import of functionality from other modules.
For instance, if we have a file sayHello.js exporting a function:
// ๐ sayHello.js
export function sayHello(user) {
alert(Hello, ${user}!
);
}
โฆThen another file may import and use it:
// ๐ main.js import {sayHello} from ‘./sayHello.js’;
alert(sayHello); // function… sayHello(‘John’); // Hello, John! The import directive loads the module by path ./sayHello.js relative to the current file, and assigns exported function sayHello to the corresponding variable.
Next Article: Generators