Skip to content
AgileLogics

JavaScript Minifier

Parses your JavaScript with Terser and writes it back without comments or unnecessary whitespace, optionally simplifying expressions and shortening local variable names. Code with a syntax error is reported with its line and column.

Working on a whole file? Open it in the JavaScript editor →

How it works

Paste your JavaScript into the input panel, choose whether to simplify code and shorten local names, and click Minify.

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

  1. Paste your script into the input box.
  2. Leave both options ticked for the smallest output, or untick one (see below).
  3. Click Minify. The first click takes a moment while the minifier loads.
  4. Copy the output into your .min.js file 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.longPropertyName stays, 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/g passes through intact.
  • License comments. /*! ... */, @license and @preserve comments 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

Last updated

Frequently asked questions

Is my code uploaded to a server?
No. The minifier is the Terser library running in your browser. It is downloaded the first time you click Minify, and after that your code is processed on your own device.
Will minifying change how my code behaves?
It shouldn't. Terser parses the code and rewrites it to an equivalent, shorter form. The usual exceptions are code that reads a function's own source text or depends on a function's name (Function.prototype.toString, fn.name) and code that reaches local variables through eval or with.
Why are my function and variable names still long?
Only local names are shortened: parameters and variables declared inside functions. Top-level names are kept because another script on the page may call them, and property names such as user.firstName are never renamed.
Does it support ES modules, classes and async code?
Yes. Modern syntax including classes, private fields, optional chaining, async/await, generators and BigInt is supported. Code containing import or export is treated as an ES module automatically.
Can it minify TypeScript or JSX?
No. Both are reported as syntax errors, because they aren't plain JavaScript. Compile them to JavaScript first, then minify the result.
Are license comments removed?
Comments that start with /*! or contain @license or @preserve are kept, which is the convention for license notices. Every other comment is removed.