← All blog posts

JavaScript Minifier for students — with examples

WhiteUI · A plain-English guide to the JavaScript Minifier

Coursework in JavaScript often asks you to write small functions and explain what they do, and sometimes to make a page load faster. The JavaScript Minifier is a handy tool for both jobs: it shows you what a browser actually needs from your code, and it gives you exact byte counts you can quote in your work. The examples below use the same numbers your coursework will, so you can check your own results against them.

At a glance

square function: 43 bytes0 bytes46 bytes92 bytes92 → 43 bytes, 49 saved (53%)
A square function minifies from 92 bytes to 43 bytes, saving 49 bytes, or 53 percent.

Example 1: a square function, 92 bytes to 43

Write a function that squares a number, with a comment on nearly every line, and it is 92 bytes long. Minified, it becomes function square(n){return n * n;}square(6); which is 43 bytes, saving 49 bytes, or 53 percent. Both comments are gone, the line breaks are gone, and the spaces around the braces are gone. The spaces around the * stay, because * is not one of the punctuation marks whose spaces are removed. And square(6) still returns 36, because nothing about what the code does has changed — only the parts people need to read it are gone.

Example 2: an add function, 59 bytes to 32

Now a function that adds two numbers, with one comment and four lines, 59 bytes in total. Minified, it becomes function add(a,b){return a + b;} which is 32 bytes, saving 27 bytes, or 46 percent. The comment is gone, the space after the comma is gone, and the spaces around the braces are gone. The spaces around the + stay, because the tool never removes spaces around + and -. Two functions, two different percentages — 53 and 46 — because the saving depends on how much of the file was comments and spacing.

Example 3: an arrow function, 42 bytes to 34

Try a modern arrow function with no comments at all: const double = (n) => { return n * 2; }; which is 42 bytes. Minified, it becomes const double=(n)=>{return n * 2;}; which is 34 bytes, saving only 8 bytes, or 19 percent. There was almost nothing to remove: no comments, no blank lines, and only the spaces around the equals sign and the braces. That small saving is a useful lesson in itself — a file that is already tight cannot shrink much, and no tool can change that.

What minifying teaches you

Reading minified code teaches you what a browser actually needs: the words, the punctuation, and the strings. Everything else — the comments, the line breaks, the careful spacing — is there for people, not for browsers. That is also why comments matter in the code you hand in: a marker reading your work needs them, even though a browser does not. The default sample makes the point well: it goes from 93 bytes to 61 bytes, and the 32 bytes removed are all reading aids.

Using the numbers in your coursework

When you write about minifying, quote the exact figures, not rough ones. The square function is 92 bytes before and 43 after, saving 49 bytes, or 53 percent. The add function is 59 before and 32 after, saving 27 bytes, or 46 percent. The arrow function is 42 before and 34 after, saving 8 bytes, or 19 percent. Those three examples together show the pattern better than any single number: commented code saves a lot, dense code saves a little, and the tool never changes what the code does.

The common mistake

Students sometimes hand in minified code as coursework, or paste it into an assignment as if it were their own finished work. It is not readable, it has no comments, and a marker who reads it will know at once that something is missing. Use the minifier to learn what a browser needs and to shrink the copy that goes on a live page, but hand in the original, with its comments and its spacing, so a person can read it.

Frequently asked questions

How much does a square function save when minified?

From 92 bytes to 43 bytes, saving 49 bytes, or 53 percent. square(6) still returns 36.

How much does an add function save?

From 59 bytes to 32 bytes, saving 27 bytes, or 46 percent.

Why does an arrow function save so little?

Because it has no comments and almost no spare spaces: 42 bytes becomes 34, saving only 8 bytes, or 19 percent.

Should I hand in minified code?

No. Hand in the readable original with its comments. Use the minified copy only for a live page.

More guides on the JavaScript Minifier

Try it yourself

Try the JavaScript Minifier — free, in your browser.

Open the JavaScript Minifier