← Back to Lessons
Edit on Github

JavaScript Import and Export of Modules

☝ This lesson is for users of WebPack and ECMAScript Modules (ESM) syntax;. There is another way to work with modules using the [CommonJS syntax] (https://requirejs.org/docs/commonjs.html) that we will not cover.

All of our JS code cannot be on the same file; that will make it hard to read and almost impossible to maintain.

Thanks to Webpack, we can split our code in small files however we want and then we are able to reference other files from our current one.

We have actually been doing that already when we import our styles, bootstrap or jQuery from the index.js.

This is how "import" and "export" works:

  • You use the word import to bring variables, classes or functions from other files.
  • You use the word export to expose variables, classes or functions to be used by other files.

For example, here we are importing a function from another file:

javascript import

javascript import

Exporting by default

There is one small variation that you can find over the internet that is called "export default" – this is just a way of exporting one thing by default onto your file.

You can only export ONE variable by default, and you don’t have to use the curly brackets while importing.

Importing the default

//content on index.js import multiplyFunction from './my_file.js'; let total = multiplyFunction(3,6) console.log(total);

Exporting by default

//content on my_file.js let multiplyFunction = function(a,b){ return a*b; }; export default multiplyFunction;

Final Example:

Here is a small demonstration of all the import/export types working on the same project.

Click to open demo in a new window

Subscribe for more!






The most efficient way to learn: Join a cohort with classmates like yourself, live streamings, coding jam sessions, live mentorships with real experts and keep the motivation.


From zero to getting paid as a developer, learn the skills of the present and future. Boost your professional career and get hired by a tech company.


Start a career in data science and analytics. A hands-on approach with interactive exercises, chat support, and access to mentorships.


Keep your motivation with this 30 day challenge. Join hundreds of other developers coding a little every day.


Start with Python and Data Science, Machine Learning, Deep Learning and maintaining a production environment in A.I.

©4Geeks Academy LLC 2019

Privacy policies

Cookies policies

Terms & Conditions