- Lab
- Core Tech

Guided: Working with Modules in Modern JS
In this Guided Code Lab, you will learn how to split your JavaScript code into modules to make it more reusable, scalable, and maintainable. Learning how to use modules in modern JavaScript is an essential skill for JS developers.

Path Info
Table of Contents
-
Challenge
Introduction
In this lab, you will take the existing code in the
app.js
file and break it into multiple files by importing and exporting the code as JavaScript modules.The lab already has the code setup in the
app.js
file. For simplicity, the functionality appears in the browser console, rather than on the page.The code simulates an online shopping cart, including data, cart functions, and output to the console with formatted pricing.
To run the app you can: Go to your app or click the Open in new browser tab button in the Web Browser tab. Then you can go to developer tools in your browser to view the console.
It's time to begin!
-
Challenge
Products Data Module
In this first step of the lab, you will move the products data array found at the top of
app.js
into its ownproducts.js
file, and import that file into theapp.js
file.Moving code into its own JS file and exporting that code to make it available for other files to use is a great way to make your code reusable and also prevent the variables within it from colliding with other variables of the same name.
In the
index.htm
file, theapp.js
file withtype=module
is already referenced, which tells the browser that you will export and import modules starting with that file.The
products.js
file already exists for you with aTODO
comment. Note that the file already has the needed final lineexport default products;
.This makes the
products
array available for other files to use. By adding thedefault
keyword, other files don't need to know the real name of the variable. When other filesimport
from theproducts.js
file, theproducts
variable is what they will receive by default.To run the app you can: Go to your app Now that
products.js
has the data, you need theapp.js
file to be able to use it. That means loading theproducts.js
file from theapp.js
file by importing the module exported from it. -
Challenge
Aliasing Functions
In this step, you will move all of the cart-related functions into their own module called
cart.js
.The
cart.js
file already exists and already has the first and last lines of code prepared for you. It importsproducts.js
at the top, since the functions use the product data. It also, at the bottom of the file, exports both adefault
value (you can only have one default) and thecalculateTotal
andremoveItem
functions. To export those other functions, it wraps them as an object inside curly braces.Lastly, the function is aliased using the
as
keyword. This means that when other files import those functions, they don't referencecalculateTotal
, but instead they referencegetTotal
.To run the app you can: Go to your app Now that the functions are in the
cart.js
file, you'll need to import them into theapp.js
file. Notice thataddToCart
can be imported directly since it is the default export. However,formatPrice
andgetTotal
must be imported using destructuring (curly braces) because they are named exports. This approach allows you to import bothformatPrice
and the aliasedgetTotal
function. Look at the last line ofcart.js
to see how they are exported.In
cart.js
,calculateTotal
is aliased using theas
keyword, exporting it with a different name,getTotal
. You will now use that aliased function name. -
Challenge
Utility Functions
In this step, you will extract two cart functions into a utility module, allowing them to be imported independently without importing the entire cart module.
The
utils.js
file has already been prepared for you with a finalexport
line in the file. It is missing theimport
forproducts
.To run the app you can: Go to your app You need to move some of the functions from the
cart.js
file into theutils.js
file, creating a reusable utilities module. You still need to use those functions in thecart.js
file so you need to import them. You have moved things around in your modules, so you need to update theapp.js
file to make sure you're importing the functions from the correct files. Finally, theapp.js
file also needs one of the utils functions! You now have a nice utilities function module! Great job! -
Challenge
Cart Operations Module
In this step, you will add a separate cart operations module that takes certain functions from the
cart.js
file and either wraps them in new functions or simply exports them as-is.In this way, you'll see how you can use modules to manage function names and function organization.
The
cartOperations.js
file is already prepared for you, but missing the import of the functions from thecart.js
file.To run the app you can: Go to your app Next, you will use the
cartOperations.js
file instead of thecart.js
file. Now, you need to make sure your code is updated to use your new wrapper functions. You've wrapped and renamed your cart functions by changing the cart API when imported by theapp.js
file. Great job! -
Challenge
Discount Functionality
In this last step, you will add discount functionality to the cart.
There is already a
specialOffers.js
file with a function created and exported. It updates a cart array and applies discounts.You will import this function and include it when calculating totals.
To run the app you can: Go to your app Next, you will apply the discount. The cart is complete and the functionality is split up amongst various modules. Congratulations, you've completed this lab!
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.