Minification is meant to be the safe, boring last step before a script ships. Yet "the site broke when we minified it" is a familiar story, and the fault is rarely in the code itself. It sits in how the minifying was done: a tool that guesses instead of reading, a hand edit that went one deletion too far, or an expectation the minifier never promised to meet. This guide walks through the mistakes that actually break sites, using the free JavaScript Minifier on Ovatly as the reference point, because the way it works (tokenize first, remove only what cannot change meaning) is exactly what the broken approaches skip.
Contents
- Quick answer
- What careful minification removes, and what it leaves alone
- Where the breakage actually comes from
- How to minify JavaScript, step by step
- Worked example: comments out, strings untouched
- Limitations and alternatives
- Common mistakes
- FAQs
- Conclusion
Quick answer
The JavaScript Minifier removes comments and unnecessary whitespace, and nothing else. It tokenizes your code first, so text inside strings, template literals and regular expressions is never mistaken for a comment, and it preserves the statement newlines that automatic semicolon insertion depends on. Paste your JavaScript, press Minify JavaScript, check the size report, then copy or download the result. Most minification breakage comes from the opposite approach: naive find-and-replace tools, hand deletion, unsupported syntax such as JSX or TypeScript, output that was never tested, and expecting renaming or dead code removal from a tool that does neither.
What careful minification removes, and what it leaves alone
A minifier has a narrow job: take out the parts of a file that exist for the reader, not the machine. Comments go. Spare whitespace goes. Everything else stays, including every variable name, every statement and every string exactly as written. The Ovatly minifier is explicit about this scope: it is a whitespace and comment minifier, not an optimizing compiler, so it does not shorten variable names, remove dead code or bundle modules.
The safety comes from how it reads before it deletes. Instead of scanning raw text, it tokenizes the code first, which means it knows whether a pair of slashes starts a comment or sits inside a quoted URL, and whether a stretch of text is a template literal whose contents are literal data. Template literals are preserved verbatim, including the expressions inside them, so they are never reshaped. That one design decision is the difference between a smaller file and a broken one.
Where the breakage actually comes from
The classic failure is the naive pass. A script or a quick hand edit that treats every // as the start of a comment will happily delete one that lives inside a string, and take the rest of that line with it. The result is a file that is smaller, syntactically wrong, and broken in a place that has nothing to do with the original code's logic.
The second failure is subtler. JavaScript's automatic semicolon insertion lets some statements end at a newline instead of a semicolon, so a pass that collapses every newline can merge two statements into one broken line. A careful tokenizer keeps the newlines that insertion depends on, which is why the minifier's output preserves statement newlines even while it strips other whitespace.
The third failure is feeding the tool something it never claimed to parse. JSX components and TypeScript annotations are not plain JavaScript, and this tool states plainly that they are not supported; they need a real parser first. When minified output from unsupported syntax misbehaves, the mistake was made at the paste, not in the minifying.
How to minify JavaScript, step by step
- Paste your JavaScript into the input box of the JavaScript Minifier.
- Press Minify JavaScript.
- Check the size report to see the saving.
- Copy or download the minified code.
Two habits belong with those steps. Keep your readable source as the file you edit, and treat the minified output as a build product. And before the output ships, run your test suite: the tool is conservative, but the entry's own advice is to test anything important after minifying it.
Worked example: comments out, strings untouched
The tool's own example shows the failure it is built to avoid. The input is:
const url = "https://example.com//not-a-comment"; // real comment
const x = 1;
The output is:
const url="https://example.com//not-a-comment";const x=1;
The // inside the string survives, because it is data, not a comment. The real comment after the statement is gone, the spaces around each = are gone, and the two statements sit on one line. A naive find-and-replace would have cut the line at the slashes inside the URL and produced a file that no longer runs; the tokenizer knew the difference before removing anything.
Limitations and alternatives
The honest limits follow from the narrow scope. Because the tool does not rewrite your program, its saving is smaller than an optimizing compiler's: terser also renames variables, folds constants and removes dead code, and a production pipeline that needs those steps should use a tool like terser instead. This minifier is for plain JavaScript only, so JSX and TypeScript need a real parser before they can be minified here.
Two edge cases are worth knowing. Unterminated strings, template literals, block comments or regular expressions are reported as errors instead of being silently mangled, so broken input surfaces as a message rather than as corrupted output. And a regex literal written right after the closing brace of a block is treated as division, a rare ambiguity that every lightweight tokenizer has to resolve one way. If your code leans on that pattern, test the output with extra care.
Common mistakes
- Minifying by hand or with find-and-replace. Deleting comments with a text search is how the
//inside a URL string gets eaten, along with everything after it on the line. Tokenizing first is what prevents this. - Deleting every newline. Some statements rely on automatic semicolon insertion at a line break. Collapse all of them and two statements can fuse into one that no longer means what either meant alone.
- Pasting JSX or TypeScript and blaming the tool. The minifier supports plain JavaScript only. Syntax it cannot parse needs a real parser first; broken output from unsupported input is an input mistake.
- Shipping minified output without testing it. The tokenizer only removes what cannot change meaning, and still the standing advice is to run your test suite after minifying anything important. Skipping that step turns a small risk into an unforced outage.
- Editing the minified file directly. A fix typed into minified output vanishes the next time you minify the source again. Edit the readable source, then regenerate the minified copy.
- Expecting renaming or dead code removal. A whitespace and comment minifier does neither, by design. If you need shorter variable names or unused code stripped, that is an optimizing compiler's job, not this tool's.
FAQs
Is the output of the JavaScript Minifier safe to run? The tokenizer is conservative: it only removes comments and whitespace that cannot change meaning, and preserves statement newlines so automatic semicolon insertion behaves as before. Still, run your test suite after minifying anything important.
Why is the saving smaller than terser? Terser also renames variables, folds constants and removes dead code. This tool deliberately avoids rewriting your program; it just removes comments and spare whitespace.
Does the JavaScript Minifier support JSX or TypeScript? No. Paste plain JavaScript. JSX and TypeScript syntax need a real parser first.
What happens if my code has an unterminated string or comment? Unterminated strings, template literals, block comments and regular expressions are reported as errors instead of being silently mangled, so you see the problem rather than shipping it.
Will minifying rename my variables or remove unused code? No. This is a whitespace and comment minifier, not an optimizing compiler. It does not shorten variable names, remove dead code or bundle modules; a tool like terser in your build pipeline goes further.
Conclusion
Minification breaks sites when it is done by guessing: text searches that cannot tell a string from a comment, newline removal that ignores semicolon insertion, unsupported syntax, and output nobody tested. Done by tokenizing, with a scope limited to comments and spare whitespace, it is the safe step it was meant to be. For the neighbouring question of whether smaller also means hidden, the companion guide on minify vs obfuscate JavaScript draws that line, and the rest of this series lives in the Developer Tools guides archive.