JavaScript Minifier / Compressor
Compresses JavaScript with no side-effects: removes whitespace and comments, shortens local variable names and applies safe compression optimisations (powered by Terser).
Result
About the JavaScript minifier
Minification shrinks a script as much as possible without changing what it does. Smaller files download faster and parse faster, which matters on mobile connections and for pages loaded millions of times. This tool runs Terser, the compressor used by most build tools (webpack, Rollup, Vite…), entirely in your browser: the code never leaves your machine. Keep the readable source for development and minify only the file you deploy.
What the minifier does
- Whitespace and comments are removed, semicolons are dropped when the grammar allows it, and string quotes are normalized.
- Mangling renames local variables, function parameters and inner functions to one- or two-letter names (
customerAddressbecomesc). Names visible from outside (global variables, exported symbols, object property names) are never renamed, so the public behaviour of the script is unchanged. - Compression rewrites the code into an equivalent but shorter form: it folds constants (
2 * 60 * 60becomes7200), removes unreachable code and unused variables, joins consecutivevardeclarations, turnsif/elseinto ternaries, inlines single-use functions, convertstrueinto!0, and so on. Only transformations that are safe in every runtime are applied. - License comments: comments starting with
/*!or containing@licenseor@preservecan be kept, which is required by most open-source licenses.
Options
| Option | Effect |
|---|---|
| ECMAScript output target | Tells Terser which syntax it may use in the output. With ES2015 or later it can, for example, shorten {a: a} into {a} and function(x){return x} into x=>x. Pick ES5 if the script must run in very old browsers and is already ES5. |
| Mangle local variable names | Renames local identifiers. This usually gives the largest saving. Disable it to keep stack traces readable. |
| Compress | Applies the compression passes described above. Disable it to only strip whitespace and comments. |
| Keep license comments | Keeps /*! … */, @license and @preserve comments. All other comments are always removed. |
| Treat as ES module | Parses import/export statements and top-level await, and assumes strict mode (which allows a few extra optimizations). |
| Keep function names | Prevents renaming of function and class names, for code that reads fn.name or relies on names for logging. |
Example
function calculateTotal(items, taxRate) {
// sum the prices
var total = 0;
for (var i = 0; i < items.length; i++) {
total = total + items[i].price;
}
var withTax = total * (1 + taxRate);
return withTax;
}
minified with mangling and compression:
function calculateTotal(t,a){for(var r=0,l=0;l<t.length;l++)r+=t[l].price;return r*(1+a)}
Syntax errors
Terser must parse the file to minify it. If the script contains a syntax error, the exact line and column are reported together with an excerpt of the source, which makes this tool a handy syntax checker as well.