Skip to content
Buy me a coffee
Minifiers / Beautifiers

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).

0 characters · 0 lines
Option 2: Or upload your JavaScript file
Newer targets enable shorter syntax (arrow functions, object shorthand, optional chaining…). Terser never transpiles: choose a target your code already complies with.
Ctrl + Enter

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 (customerAddress becomes c). 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 * 60 becomes 7200), removes unreachable code and unused variables, joins consecutive var declarations, turns if/else into ternaries, inlines single-use functions, converts true into !0, and so on. Only transformations that are safe in every runtime are applied.
  • License comments: comments starting with /*! or containing @license or @preserve can be kept, which is required by most open-source licenses.

Options

OptionEffect
ECMAScript output targetTells 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 namesRenames local identifiers. This usually gives the largest saving. Disable it to keep stack traces readable.
CompressApplies the compression passes described above. Disable it to only strip whitespace and comments.
Keep license commentsKeeps /*! … */, @license and @preserve comments. All other comments are always removed.
Treat as ES moduleParses import/export statements and top-level await, and assumes strict mode (which allows a few extra optimizations).
Keep function namesPrevents 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.