Minification gets blamed for a lot of bugs it never caused. A script works fine in development, you run it through a minifier, and suddenly it throws in production. The minifier must have mangled something, right?

Most of the time, no. The minifier just removed the whitespace that was secretly holding your code together. The real culprit is automatic semicolon insertion, and it was already in your code, waiting.

The newline that was doing real work

JavaScript inserts semicolons for you when it thinks a statement has ended. That is convenient right up until a newline is the only thing separating two statements that should not be joined.

Take this:

const total = price + tax (total > 100 ? applyDiscount() : checkout()) 

With the newline in place, JavaScript reads two statements. Now minify it by joining the lines:

const total = price + tax(total > 100 ? applyDiscount() : checkout()) 

That is one statement now, and it calls tax as a function. tax is a number, so you get tax is not a function in production, on a line that looks nothing like your source.

A good minifier knows this and keeps a newline or a semicolon where ASI matters. A naive whitespace stripper does not, and this is the exact bug that makes people say "minifiers are unsafe."

What a minifier actually removes

A real minifier does three jobs, in order of how much they save:

  1. Comments and whitespace. The biggest, safest win. Nothing about how the code runs changes.
  2. Shortening names. Local variables like shoppingCartTotal become t. Top-level names are usually left alone, because another script might use them.
  3. Rewriting expressions. if (false) { ... } disappears. true becomes !0. const x = 1 + 2 becomes const x = 3. This is the part that needs a real parser, because it has to prove the rewrite is safe before doing it.

That third job is why a parser-based minifier (Terser is the one behind webpack and Vite) beats a text-based one. A text tool can only guess where a string ends and a comment begins. A parser knows.

What minification is not

Minification is not obfuscation, and it is not security. Anyone can run your minified file through a formatter and get readable code back in seconds. If the code contains a secret, minifying it does not hide the secret. It only makes the file smaller.

It is also not a substitute for a bundler. If your project has a build step, let the bundler minify. An online minifier is for the one-off jobs: a tag manager snippet, a CMS embed, a bookmarklet, a quick check of how small a script can get.

The habit that prevents the whole class of bugs

Write semicolons, or use a formatter that inserts them. That is the entire fix. If every statement ends with an explicit semicolon, no minifier can ever join two statements by accident, because there is nothing ambiguous left to join.

And before you ship a minified one-off script, paste it back into your browser console and run it once. Thirty seconds of checking beats a production error every time.

If you want to try it on your own code, this free JavaScript minifier runs entirely in your browser: JavaScript Minifier. Paste a script, toggle mangling and compression, and watch the byte count drop. Your code never leaves your device, and if it has a syntax error you get the parser's own message instead of broken output.