What is a JavaScript minifier?
A JavaScript minifier rewrites a script so it does the same thing in fewer bytes. It removes comments, line breaks and indentation, and can also simplify expressions and rename local variables to single letters. Browsers run the result exactly as before, but the file downloads and parses faster. This tool uses Terser, the minifier behind many JavaScript build tools.
Terser parses your code into a syntax tree before changing anything. That matters for JavaScript, where a / can start a regular expression or mean division, and where a line break can end a statement. Find-and-replace minifiers get those cases wrong.
How to minify JavaScript
- Paste your script into the input box.
- Leave both options ticked for the smallest output, or untick one (see below).
- Click Minify. The first click takes a moment while the minifier loads.
- Copy the output into your
.min.jsfile or<script>tag.
The two options
| Option | Ticked (default) | Unticked |
|---|---|---|
| Simplify code (compress) | Rewrites code to shorter equivalents: true becomes !0, 1 + 2 becomes 3, unreachable branches and unused local variables are dropped |
Code structure is kept as written; only whitespace and comments go |
| Shorten local variable names (mangle) | Parameters and variables inside functions become e, t, n... |
All names stay as written, which is easier to debug |
Untick both and you get whitespace-and-comment removal only.
Worked example
Input (227 characters):
// Adds tax to a price
function addTax(price, rate) {
const tax = price * rate;
/* round to cents */
return Math.round((price + tax) * 100) / 100;
}
const total = addTax(19.99, 0.2);
console.log(`Total: ${total}`);
Output with both options on (126 characters, about 44% smaller):
function addTax(o,t){const a=o*t;return Math.round(100*(o+a))/100}const total=addTax(19.99,.2);console.log(`Total: ${total}`);
Both comments are gone, price, rate and tax became o, t and a, and 0.2 lost its leading zero. addTax and total kept their names because they are declared at the top level. With mangling off, the same input comes out at 148 characters.
What it leaves alone
- Top-level names. Global functions and variables are not renamed.
- Property names.
obj.longPropertyNamestays, since renaming it could break JSON, the DOM or other scripts. - Strings and template literals. Text is preserved, although quotes may change from
'to". - Regular expressions.
/\/\/x/gpasses through intact. - License comments.
/*! ... */,@licenseand@preservecomments are kept. - Functions that call
eval. Local names inside them aren't shortened, because the evaluated code may refer to them.
Syntax errors
Code has to parse before it can be minified. If it doesn't, you get the parser's message with a position, for example:
Invalid JavaScript: Unexpected token: punc (;) (line 2, column 9)
punc means punctuation, name an identifier and eof the end of the input, which usually points to an unclosed bracket or string. For a second opinion on a confusing error, the JavaScript validator reports your own browser's wording.
When to use this, and when to use a build tool
For an application with a build step, minification should happen there: Vite, webpack, Rollup and esbuild all minify production builds and can emit source maps for debugging. This page doesn't produce source maps. It suits everything else: a snippet for a CMS or tag manager, a bookmarklet, an inline script in an email or landing page, or a quick check of how much a file would shrink.
Minified files compress further in transit. Serving JavaScript with gzip or Brotli (MDN: HTTP compression) typically saves more than minifying, and the two add up.
Related tools
- Code beautifier: re-indent minified JavaScript so it is readable again.
- CSS minifier and HTML minifier: shrink the rest of the page.
- JavaScript editor: write and edit scripts with syntax highlighting.
- Learn JavaScript: lessons on the language itself.
Last updated