JavaScript Minifier
Minify JavaScript code to reduce file size
Tip: Paste your JavaScript code and click Minify to compress it by removing whitespace and comments, reducing file size for faster loading.
Runs in your browserInput JavaScript Code
Minify JavaScript with Terser
Paste JavaScript or open a .js file and press Minify Code. The JavaScript minifier runs Terser 5.31 in your browser: it removes comments and whitespace, gives variables and functions short names, and rewrites code into shorter forms. The result shows the size before and after, and the percentage saved.
Minifying JavaScript this way starts by parsing it, so code with a syntax error is not minified. You get an alert with the parser's message instead.
JavaScript minifier example
Real output for a small module: 329 bytes become 125, 62% smaller.
/*! cart.js v1.2 | MIT */
// Add up a cart, with an optional discount
function cartTotal(items, discount) {
let total = 0;
for (const item of items) {
total += item.price * item.qty;
}
if (discount) {
total = total * (1 - discount);
}
debugger;
return Math.round(total * 100) / 100;
}
export { cartTotal };
function cartTotal(t,o){let r=0;for(const o of t)r+=o.price*o.qty;return o&&(r*=1-o),Math.round(100*r)/100}export{cartTotal};
Both comments are gone, the license comment too, and so is the debugger statement. The if became o&&(r*=1-o). Inside the function every name is now one letter, while cartTotal keeps its name, so code that calls or imports it keeps working.
Which names change
Top-level functions, classes and variables keep their names and stay in the file even when nothing in it calls them, so a function used from an onclick attribute or another script still works. Files with import and export work too. Only names inside functions get shorter.
Kept
- Top-level names, used or not
- Imports and exports, under their own names
consolecalls and property names
Removed or renamed
- Every comment, including
/*! */license comments debuggerstatements and code that can never run- Names of parameters and local variables
Source maps, compression and caching
No source map is produced, so an error in the minified file points to line 1 and a one-letter name. Keep the original file for debugging, and use your build tool when you need a map.
Servers usually send .js files with gzip or brotli compression as well. Minifying still pays off, because renamed variables and removed code are bytes that compression can't take out, and the browser has less code to parse.
When you replace a minified file that browsers have cached, change its name or add a version, as what is cache busting explains.
Need to read minified JavaScript?
The JavaScript beautifier puts the line breaks and indentation back. Renamed variables keep their short names.
JavaScript minifier questions
Not at the top level: functions, classes and variables there keep their names, so other scripts can still call them. Parameters and variables inside a function are renamed to one or two letters.
Code that reads names at run time can, for example fn.name or constructor.name, since those names are shortened. Lines without semicolons are not a risk: Terser parses the code before it joins anything. Test the minified file before you ship it.
No. console calls are kept; only debugger statements are removed.
Paste the whole page into the HTML minifier. It runs inline <script> blocks through Terser and minifies the HTML around them.
No. Minifying, Terser included, runs in your browser.