Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is bundling in JavaScript

The Concept of Bundling in JavaScript

When starting out in programming, there are many things to learn, and one of the many important topics in JavaScript is the concept of bundling. In layman's terms, bundling is just like a school bag where you keep all your books, notebooks, and stationary. In JavaScript, our 'bag' is the bundle, and the 'books' and 'stationary' are our different JavaScript files and modules.

Why Do We Need Bundling?

Imagine if you had to carry each of your books, notebooks, and pens separately to school. That would be so inconvenient, right? This is where your school bag comes into play. You can keep everything in it, and just carry the bag. The same is the case with JavaScript. When you're working on a big project, you may have hundreds of JavaScript files. Loading each one separately would be time-consuming and inefficient. So, we bundle them all into one single file which can be loaded once, saving time and resources.

How Does Bundling Work?

To understand how bundling works, let's first talk about modules. In JavaScript, a module is just like a chapter in your book. Each chapter (module) has its own topic (functionality), and all of these chapters (modules) together make up your book (JavaScript program).

When you bundle, you are essentially taking all these chapters (modules), ripping them out of their books (files), and combining them to form a new book (bundle). This new book (bundle) then contains all the information (code) of the original books (files).

Here's a simple code example:

Suppose you have two JavaScript files:

// math.js
export function add(a, b) {
  return a + b;

// message.js
import { add } from './math.js';

export function displaySum(a, b) {
  const sum = add(a, b);
  console.log(`The sum is ${sum}`);

You can bundle these files into one:

// bundle.js

// Contents of math.js
function add(a, b) {
  return a + b;

// Contents of message.js
function displaySum(a, b) {
  const sum = add(a, b);
  console.log(`The sum is ${sum}`);

As you can see, the add function from math.js and the displaySum function from message.js have been combined into one file: bundle.js.

Tools for Bundling

Just as there are different types of bags available according to your needs, there are also different tools available for bundling your JavaScript files. Some of the popular ones include Webpack, Rollup, and Parcel. These tools not only help you bundle your JavaScript files but also provide features like code splitting, lazy loading, and more.


To wrap up, bundling is an essential part of JavaScript programming that makes our code efficient and manageable. It's like a magical bag that holds all our code 'books' together, making it easier to carry around and use. Although it might seem complicated at first, with a little practice and patience, it will soon become second nature. So, go ahead and start bundling, and see the magic unfold in your code!