The Difference Between the Import function and the Import Statement
Tags: javascript, webdev, productivity, tech
Published: 2020-09-24 16:00:35

In my new [Fundamentals of Angular](https://yj685.infusion-links.com/api/v1/click/6184021656338432/5570275139911680) course that is being released later this week, I cover lazy loading with Angular feature modules. In the section on lazy loading, we encounter the import() function. One of the things that can be confusing about this function is how it compares to the more common import statement.

I've previously written about the import statement and how it can be confusing. If you want a deeper dive on that, check out my blog [here](https://yj685.infusion-links.com/api/v1/click/5492875435180032/5570275139911680).

Let's take a quick look at the import statement and the import function in JavaScript.

The import statement is what allows us to divide up our JavaScript into multiple files without using Script tags and ugly globals.

Generally, the import statement looks something like this and appears at the top of our files:

![image](https://cdn-images-1.medium.com/max/800/0*YO27KmlHbu2V5YZv?)

This statement is a static import. Whatever engine is processing our JavaScript to bundle up for delivery to the browser will crawl all the import statements we have in our files, and create one large bundle. In general, this is desirable. It allows for static analysis of our files, and advanced optimizations like tree shaking and dead code removal.

But it doesn't allow us to either delay the inclusion of some code into our client-side project, or delay the download and loading of some of our code.

As projects get bigger and more complex and we use larger and larger frameworks and more and more third-party tools, our initial bundle can get unreasonably large. So finding the parts of our application that aren't needed initially, and downloading them later can improve the initial performance of our application. And if the code is never needed, then we don't have to force the user to download it.

This functionality is enabled with the import function.

The import function uses quite a different syntax than the static import statement.

It looks like this:

![image](https://cdn-images-1.medium.com/max/800/0*9hSUjLyeYGV_Ifoa?)

Let's go over this syntax, and what it's doing.

First of all, the module path is probably the most likely place you'll get something messed up. Getting the path wrong is easy since the path has to make sense to the server. So correctly getting the path to the module set is really the biggest potential mistake. You can either give a complete path to the file, or you can give a relative path in which case the browser will assume your base URL from your current domain. Any import statement executed like this will show up in the network tab of your browser as a typical JavaScript request.

Once the module has been downloaded, you now have a handle to the module itself and can do whatever you want with it in the then callback function of the promise. Note that the downloaded module is immediately parsed and executed by the browser without you doing anything. So if the code has any side effects, you can't stop them from happening.

This is the mechanism used by many frameworks and libraries to download code on demand whenever needed.

We use XHR calls to gather data, but we use the dynamic import function to gather additional code our project may need.

The import function is used far less often than the static import statement, but it's still extremely useful to understand.

Happy coding!

Signup for my newsletter [here](https://thinkster.io/?previewmodal=signup?utm_source=devto&utm_medium=blog&utm_term=importfunctionvsimportstatement&utm_content=&utm_campaign=blog).

Visit Us: [thinkster.io](https://thinkster.io?utm_source=devto&utm_medium=blog&utm_term=importfunctionvsimportstatement&utm_content=&utm_campaign=blog) | Facebook: @gothinkster | Twitter: @GoThinkster