How to minify JavaScript
- Paste your JavaScript into the box above.
- Leave licence banners enabled if the code includes any third-party notices.
- Press Minify JavaScript and read the reported saving.
- Test the minified file before shipping, always, with any minifier.
- Copy the result, or download it as a .min.js file.
What makes this safe
- Tokenises before minifying, so a comment marker inside a string is never mistaken for a comment.
- Correctly distinguishes a regular expression literal from a division operator.
- Leaves template literals byte for byte intact, including their whitespace.
- Preserves newlines where automatic semicolon insertion depends on them.
- Never renames identifiers, so debugging and reflection still work.
- Optionally keeps licence banners that many open-source terms require.
Why JavaScript is harder to minify than CSS
CSS has no ambiguity worth worrying about. JavaScript has several, and each one is a way for a careless minifier to produce code that looks fine and fails at runtime.
The classic is the forward slash. In a / b it is division; in /ab+/g it starts a regular expression. Telling them apart requires knowing what came before, and a minifier that guesses wrong will happily delete half a pattern. The same applies to // inside a string, "https://example.com" is not a comment, though a regex-based minifier will treat it as one and truncate the line.
Automatic semicolon insertion
JavaScript inserts semicolons where it thinks you meant one, and the rules depend on line breaks. Remove the newline after a bare return and the function starts returning a value instead of undefined, or vice versa. The same risk applies after throw, break, continue and a postfix increment.
This minifier keeps a newline wherever removing it could change meaning. That costs a few bytes and removes a whole class of bug that is extremely unpleasant to diagnose in production.
What this does not do
- No renaming. Build tools shorten local variables to single letters, which is where most of their saving comes from.
- No dead code removal. Unreachable branches stay.
- No tree shaking. Unused exports are not detected.
- No transpiling. Modern syntax stays modern.
The consequence is smaller savings, typically 20 to 40 per cent rather than the 60 or 70 a bundler achieves, and complete safety. For a shipped application, use a real build tool. For a snippet, an inline script, or code you need smaller without introducing a build step, this is the right amount of tooling.
Why renaming matters more than it seems
Minifiers that rename identifiers break anything relying on names at runtime: dependency injection by parameter name, reflection over function names, error messages that reference variables, and debugging without a source map. Since this tool does not rename, a stack trace from the minified file still points at recognisable code.
It also means the output is readable enough to audit, which matters if you are minifying code you did not write. For stylesheets and markup alongside it, the CSS minifier and HTML minifier take the same conservative approach. Nothing is uploaded at any point, so proprietary code stays on your machine.
Where the real savings come from
If file size genuinely matters, minification is rarely the biggest lever. A page loading three hundred kilobytes of JavaScript is usually loading a library it uses one function from, a date-handling package that ships every locale, or a component framework pulled in for one widget.
Auditing dependencies routinely removes more than any minifier gains. After that, splitting code so a visitor downloads only what the current page needs beats compressing everything they will never run. Minification is the last few per cent, applied after those decisions, worth doing, and not where a slow page gets fixed.
Testing minified output
Whatever minifier you use, test the result rather than assuming. Run the minified file in place of the original and exercise the paths that matter, particularly anything involving regular expressions, template literals or code that inspects function names. A minifier that has never broken anything for you has simply not met your edge case yet.