

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
No active XP Potion
Accept a Quest
Login to submit answers
Back
ctrl+,
Next
ctrl+.
Back in the earliest days of JavaScript - when it was but a wee browser-only language that looked like bastardized Java - JavaScript files were small. A callback here, a dynamic element there, and a sprinkle of (gasp) jQuery to make it all work.
But as the web has grown, we're not just shipping a bit of interactivity to static HTML pages anymore. We're building full-fledged applications with crazy amounts of state, logic, and dependencies. In the case of single page applications, sometimes HTML is just a shell that gets filled in with JavaScript at runtime.
Long story short, JS needed modules. In Go, you have packages. In Python, you have modules. Now, JavaScript also has modules - they're just a way to split your code into separate files and import the code you need across a large codebase.
Modules in JavaScript exist at the file level, just like Python. So, if you have a file called math.js that looks like this:
// math.js
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
module.exports = {
add,
subtract,
};
You can import and use those functions in another file like this:
// main.js
const { add, subtract } = require("./math.js");
console.log(add(1, 2)); // 3
console.log(subtract(1, 2)); // -1
This is CommonJS syntax. I'll explain what that means soon.
Let's modularize our heifer project! Instead of defining everything in a single file, we'll split functionality into modules.
moo, NAME!
Run and submit the CLI tests.
The Boot.dev CLI requires you to be signed in to submit your solution!
Copy/paste one of the following commands into your terminal:
Run
bootdev run a7724be8-c98a-4747-b34b-fdc6dac72568
Submit
bootdev run a7724be8-c98a-4747-b34b-fdc6dac72568 -s
To run and submit the tests for this lesson, you must have an active Boot.dev membership
Become a member to view solution
Using the Bootdev CLI
The Bootdev CLI is the only way to submit your solution for this type of lesson. We need to be able to run commands in your environment to verify your solution.
You can install it here. It's a Go program hosted on GitHub, so you'll need Go installed as well. Instructions are on the GitHub page.