Why You Are Here
A script is larger than it needs to be. A plugin, a snippet, or a file you inherited, shipping comments and formatting the browser never needs.
You have no build pipeline. A static site, a CMS theme, or a landing page where nobody is running a bundler and a web tool is the practical option.
PageSpeed has told you to minify JavaScript. A common audit recommendation, and worth understanding before acting on.
What This Tool Does
The JavaScript Minifier removes comments and unnecessary whitespace from JavaScript, producing a smaller file that behaves identically.
Paste your code or upload a file, and the tool returns the minified result ready to copy or download.
Two Kinds of Minifier, Two Levels of Risk
This distinction matters more than anything else here, and most tools do not make it.
|
Whitespace-only |
AST-based |
|
|
Removes comments and spacing |
Yes |
Yes |
|
Renames variables |
NO |
Yes |
|
Rewrites expressions |
NO |
Yes, true becomes !0 |
|
Removes unreachable code |
No |
Yes |
|
Typical saving |
Moderate |
Substantially larger |
|
Risk of behaviour change |
Very low |
Low but real |
|
Needs a full parser |
No |
Yes |
Whitespace-only minification is close to risk-free
It deletes characters the engine ignores. Strings, template literals, and regular expressions are left untouched, so the program that comes out is the program that went in.
AST-based minification parses your code into a syntax tree, transforms it, and regenerates it
That is where the large savings live, since renaming local variables to single characters is the biggest single win available, and also where the edge cases live.
Neither is wrong
Which you want depends on whether you have a build pipeline that can test the output, and how much you need the extra bytes.
Automatic Semicolon Insertion - The Real Hazard
This is where naive minifiers corrupt working code, and it is worth understanding whichever tool you use. JavaScript inserts semicolons for you at certain line breaks. The rules are specific, and they mean a line break can carry meaning.
function f() {
return
42
}
This returns undefined, not 42
ASI places a semicolon after return, so the 42 is unreachable. A minifier that joins those lines produces return 42, which returns 42. Same input, different program, no error message anywhere.
Line breaks matter wherever the previous token could end a statement and the next could begin one
That is exactly the condition under which ASI fires, and it is why a minifier must either preserve those breaks or parse the code properly enough to insert the semicolons itself.
Semicolon-free code is not the problem
Standard style without semicolons is perfectly valid and survives a correct minifier intact. The problem is a minifier that treats newlines as free whitespace when sometimes they are not.
What Mangling Actually Does
If you use an AST-based minifier, variable renaming is where most of the saving comes from.
// Before
function calculate(firstNumber, secondNumber) {
const result = firstNumber + secondNumber;
return result * 2;
}
// After mangling
function calculate(a,b){return 2*(a+b)}
Local variables are renamed. Some things are not
|
Element |
Mangled by default? |
|
Local variables and parameters |
Yes |
|
Function names |
Often preserved, may be public API |
|
Object property names |
NO — would break property access |
|
Global identifiers |
No |
|
Strings |
No |
Property names are excluded for a good reason
Renaming a property to a single letter breaks anything accessing it by string, including JSON.parse output, bracket notation access, and any external code touching your objects. Minifiers that offer property mangling require an explicit reserved list, and it is easy to get wrong.
Constant folding happens too
An expression like 100 times 2 becomes 200, large numbers become scientific notation, and true becomes an exclamation mark followed by zero, all evaluated at build time rather than runtime.
Keeping function names is worth considering
Production error stacks are considerably more useful when function names survive, and the byte cost is small.
Your Real Saving Is Smaller Than the Percentage
An honest framing that headline figures obscure. Minification percentages are quoted on raw file size. A file dropping from 100 KB to 60 KB is reported as 40% smaller.
But your server compresses before sending
Gzip and Brotli both work by finding and encoding repetition, and whitespace, indentation, and repeated identifiers are exactly the kind of repetition they handle well.
|
Stage |
What it removes |
|
Minification |
Characters the engine ignores |
|
Compression |
Repetition across the whole file |
|
Overlap |
SUBSTANTIAL |
So the meaningful comparison is minified-and-compressed against unminified-and-compressed
And that gap is much narrower than the raw figure.
Minification still helps
Shorter identifiers mean fewer distinct symbols for the compressor to encode, and less code for the browser to parse. Parsing is real work on mobile devices. But the benefit is smaller than a raw percentage implies.
Worth checking your server actually compresses
If compression is not enabled, enabling it is a far larger win than minifying. It is a bigger lever than anything else on this page, and it is a configuration setting rather than a build step.
Minification Is Not Obfuscation
Mangled code is not protected code
Any beautifier reverses the formatting in seconds, and while variable names are lost, the logic is entirely readable.
|
Minification |
Obfuscation |
|
|
Purpose |
Smaller files |
Harder to understand |
|
Reversible |
Formatting, yes |
Deliberately resistant |
|
Performance cost |
None |
Frequently significant |
|
Protects your code |
NO |
Somewhat, briefly |
Anything shipped to a browser is readable
If a secret must stay secret, it belongs on a server. No client-side transformation changes that, and treating minification as a security measure is how API keys end up in public bundles.
Source Maps
Minified code is undebuggable without them, and they carry a consideration people miss.
A source map maps minified output back to your original source
So browser dev tools show real file names, line numbers, and variable names in stack traces. Without one, a production error reports a position in a single-line minified file, which tells you almost nothing.
The consideration
A publicly accessible source map exposes your original, commented, unminified source to anyone who looks. That is fine for open code and a disclosure for proprietary code.
Two common approaches
Publish source maps for open projects, or upload them to your error tracking service without serving them publicly, which gives you readable stack traces without publishing the source.
When to Use a Web Tool and When Not To
|
Situation |
Better approach |
|
One-off file or snippet |
This tool |
|
Static site with no build step |
This tool |
|
CMS theme or plugin file |
This tool |
|
Project with a bundler |
Minify in the build |
|
Anything version controlled |
Minify in the build |
|
Code needing source maps |
Minify in the build |
If you have a build pipeline, minify there
It runs automatically, produces source maps, and keeps your source file as the thing you edit. Pasting into a web tool means the minified file is what you have, and re-editing it later is painful.
Never edit minified code
Keep the original, change that, and minify again. A minified file is an output, not a source.
Common Mistakes to Avoid
Minification is one-way in practice: a beautifier restores formatting but cannot restore comments or original variable names if mangling was applied. Once the minified version is all you have, every future change means working in unreadable code. Keep the original, save the minified output alongside it as a separate file, and treat that file as a build artefact rather than something to edit.
Mangled JavaScript is not obfuscated and obfuscated JavaScript is not secure. Anything delivered to a browser can be read, reformatted, and understood, and variable names are the only thing genuinely lost. API keys, credentials, and business logic that must stay private belong on a server. Treating a minifier as a security layer is a common route to exposed secrets.
Gzip and Brotli remove much of the same redundancy minification targets, and a server that is not compressing is leaving a far larger saving on the table than minification can offer. Check that compression is enabled before optimising further, since it is a configuration change rather than a build step and it applies to every asset you serve rather than only your JavaScript.
Quick Reference
|
Item |
Detail |
|
Two kinds |
Whitespace-only and AST-based |
|
Whitespace-only |
Close to risk-free |
|
AST-based |
Saves more, carries more risk |
|
ASI |
Line breaks can carry meaning |
|
return alone on a line |
Returns undefined |
|
Property names |
Not mangled by default |
|
Mangling |
Breaks string-key property access |
|
Raw percentage |
Overstates the real saving |
|
Compression |
Removes similar redundancy |
|
Priority |
Enable gzip or Brotli first |
|
Minification |
Is not obfuscation or security |
|
Public source maps |
Expose your source |
|
Minified output |
Never edit it, edit the original |
