Menu
Coddy logo textTech

What are Modules?

Part of the Object Oriented Programming section of Coddy's JavaScript journey — lesson 7 of 56.

When your JavaScript code grows large and complex, it becomes difficult to manage everything in one file. The solution is modules: splitting your code into separate, organized files.

A module is a JavaScript file that contains related code. Each module handles one specific job, making your application easier to understand and maintain.

A JavaScript module is a regular .js file that can export code (variables, functions, classes) so other files can import and use it.

Example:

One Messy File:

// file: messy-app.js
let user = "John";
let score = 0;

function login() {
  console.log("Welcome " + user);
}

function addPoints(points) {
  score += points;
}

Two files organized with Modules:

// file: user-module.js
let user = "John";
function login() {
  console.log("Welcome " + user);
}
// file: game-module.js  
let score = 0;
function addPoints(points) {
  score += points;
}

user-module.js - It handles user authentication and user-related data and contains the username and login functionality.

game-module.js - It manages game scoring and points system and tracks the score and handles point calculations.

challenge icon

Challenge

Easy

You have two modules already connected: user-module.js and game-module.js.

Your task is to call the addPoints function with the value 10 in user-module.js.

Expected Output:

Score increased! Current score: 10

Cheat sheet

A module is a JavaScript file that contains related code. Each module handles one specific job, making your application easier to understand and maintain.

Modules allow you to split your code into separate, organized files instead of managing everything in one large file.

A JavaScript module is a regular .js file that can export code (variables, functions, classes) so other files can import and use it.

Example of organizing code with modules:

// file: user-module.js
let user = "John";
function login() {
  console.log("Welcome " + user);
}
// file: game-module.js  
let score = 0;
function addPoints(points) {
  score += points;
}

Try it yourself

import { addPoints } from './game-module.js';
let user = "John";

// TODO: Call the addPoints function with the value 10
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming