JS Modules

·

1 min read

  • JS modules helps us in organizing the our scripts and writing multi file code .

What is a module?

A module is just a file. One script is one module. As simple as that.

  • 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.

Note : All code that is not exported in a module will be executed only once and further it's imports will shared with other files.

There’s a rule: top-level module code should be used for initialization, creation of module-specific internal data structures. If we need to make something callable multiple times – we should export it as a function, like we did with sayHi above.

Source & More readings