- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- 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.
Lab Info
Table of Contents
-
Challenge
Introduction
In this lab, you will take the existing code in the
app.jsfile 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.jsfile. 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.jsinto its ownproducts.jsfile, and import that file into theapp.jsfile.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.htmfile, theapp.jsfile withtype=moduleis already referenced, which tells the browser that you will export and import modules starting with that file.The
products.jsfile already exists for you with aTODOcomment. Note that the file already has the needed final lineexport default products;.This makes the
productsarray available for other files to use. By adding thedefaultkeyword, other files don't need to know the real name of the variable. When other filesimportfrom theproducts.jsfile, theproductsvariable is what they will receive by default.To run the app you can: Go to your app Now that
products.jshas the data, you need theapp.jsfile to be able to use it. That means loading theproducts.jsfile from theapp.jsfile 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.jsfile already exists and already has the first and last lines of code prepared for you. It importsproducts.jsat the top, since the functions use the product data. It also, at the bottom of the file, exports both adefaultvalue (you can only have one default) and thecalculateTotalandremoveItemfunctions. To export those other functions, it wraps them as an object inside curly braces.Lastly, the function is aliased using the
askeyword. 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.jsfile, you'll need to import them into theapp.jsfile. Notice thataddToCartcan be imported directly since it is the default export. However,formatPriceandgetTotalmust be imported using destructuring (curly braces) because they are named exports. This approach allows you to import bothformatPriceand the aliasedgetTotalfunction. Look at the last line ofcart.jsto see how they are exported.In
cart.js,calculateTotalis aliased using theaskeyword, 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.jsfile has already been prepared for you with a finalexportline in the file. It is missing theimportforproducts.To run the app you can: Go to your app You need to move some of the functions from the
cart.jsfile into theutils.jsfile, creating a reusable utilities module. You still need to use those functions in thecart.jsfile so you need to import them. You have moved things around in your modules, so you need to update theapp.jsfile to make sure you're importing the functions from the correct files. Finally, theapp.jsfile 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.jsfile 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.jsfile is already prepared for you, but missing the import of the functions from thecart.jsfile.To run the app you can: Go to your app Next, you will use the
cartOperations.jsfile instead of thecart.jsfile. 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.jsfile. Great job! -
Challenge
Discount Functionality
In this last step, you will add discount functionality to the cart.
There is already a
specialOffers.jsfile 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!
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.