Introduction
If you’ve spent any time working with Javascript over the past 5 or so years, you’ve probably come across Javascript Imports and Exports. Introduced as part of the ECMAScript 6 (ES6) specification in June 2015 and now supported in all major browsers, Imports and Exports have shaped the way we build modern Javascript applications. In the beginning, Javascript wasn’t designed to be modular. However, 2009 saw the release of Node JS, a popular JavaScript runtime environment that runs outside the browser. Node JS soon began supporting modularity through the CommonJS require
function and with this developers began searching for solutions to bring modularity to Javascript in the browser.
What is modularity in Javascript
A Javascript module is a file that can export variables
, functions
, and classes
that can be imported anywhere, allowing developers to share code across multiple files or projects.
Exporting from Javascript modules
Exporting variables
, functions
, and classes
is made simple in Javascript with the export
keyword, allowing developers to specify which data/functionality they would like accessed outside the module. Take the following example of a Dice module
: