← All blog posts

JavaScript Minifier for beginners — start here

WhiteUI · A plain-English guide to the JavaScript Minifier

If you have written your first JavaScript and heard someone say you should minify it before it goes on a website, this guide starts from zero. Minifying means taking code written for people to read and squeezing out the parts only people need, so the file is smaller and loads faster. The JavaScript Minifier does that for you in seconds, and this page walks through your first three tries.

At a glance

square function: 53% smaller43 bytes minified49 bytes saved92 bytes → 43 bytes, 49 saved
A square function goes from 92 bytes to 43 bytes, saving 49 bytes, or 53 percent.

What “minified” means

Code you write has comments that explain it, blank lines that separate ideas, and spaces that make it easy to scan. A browser does not need any of that: it reads the words and the punctuation, and the rest is just bytes to download. Minifying removes the comments, collapses the line breaks and takes out the spaces around punctuation, leaving the same instructions in a smaller file. The code still does the same thing — it only looks different, and it looks worse to a person, which is why you always keep your original.

Your first try: leave everything as it is

Open the tool and look before you touch anything. It already shows a sample: a short greet function of 93 bytes, with two comments and five lines. The result beside it reads 34% smaller, with the lines Original: 93 characters · Minified: 61 characters and Saved: 32 characters. That is a complete first result, and it shows what minifying does without you typing a word. Press Copy minified JS and paste the result somewhere to see what it looks like: one line, no comments, no spare spaces.

Your second try: a square function

Now try a function of your own. Type or paste this into the box, exactly as shown, and watch the numbers change. It is 92 bytes, with a comment on nearly every line, so it should shrink a lot.

// My first script
function square(n) {
  /* square a number */
  return n * n;
}
square(6);

function square(n){return n * n;}square(6);

The minified version 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.

What stays the same

Three things never change when you minify with this tool. The words in your code stay the same: square is still square and n is still n, because this tool does not rename anything. The text inside quotes stays the same: “Hello, ” keeps its trailing space. And the answer stays the same: square(6) is 36 before minifying and 36 after. If a minified file gives a different answer from the original, something went wrong, and the first thing to check is whether your code has a regular expression with repeated spaces in it, because those are the one thing this tool can change by mistake.

Where the minified code goes

Minified code belongs in the file your website actually loads, and the readable original belongs in your project folder. If your page loads a file called script.js, save the minified result as script.js and keep the original as script.src.js or in a folder called src. When you need to change the code, change the original, minify it again, and replace the loaded file. Never edit the minified copy directly: with no comments and no line breaks, a small edit is easy to get wrong.

The common mistake

Beginners often delete the original after minifying, because the minified file looks like the finished product. It is not: it is the packed version, and it is miserable to read or fix. Keep the original, with its comments and its spacing, and treat the minified file as something the tool makes for you whenever you need it. Minifying takes seconds, so there is never a reason to keep only the packed copy.

Frequently asked questions

What does minifying JavaScript mean?

Removing the parts only people need — comments, blank lines and spare spaces — so the file is smaller and loads faster, while the code still does the same thing.

How much does the sample shrink?

The sample goes from 93 bytes to 61 bytes, saving 32 bytes, or 34 percent.

Does minifying change what my code does?

No. square(6) still returns 36 after minifying, because only comments, line breaks and spare spaces are removed.

Should I keep my original code?

Yes. Always keep the readable original with its comments, and treat the minified file as a copy the tool makes for you.

More guides on the JavaScript Minifier

Try it yourself

Try the JavaScript Minifier — free, in your browser.

Open the JavaScript Minifier