Skip to content

JavaScript Minifier - Minify JS Online with Terser

JavaScript input
Minified JavaScript

Minify JavaScript with Terser

Minifying strips whitespace and comments, shortens local names and rewrites expressions into shorter equivalents. This page runs Terser in your browser. Paste code and the minified output appears after a 300 ms pause.

See exactly how much you save

Under the editor are three sizes: original, minified, and minified plus gzip. The gzip number is the one to watch, because it is roughly what goes over the wire. For a 2 KB inline script the real saving may be a few hundred bytes, which is worth knowing before you give up readable code in a template.

Check the output before you ship it

Three things catch people out:

  • Empty output. With ES module on, a file of top-level functions that nothing exports or calls is dead code, and Terser removes all of it. function helper(x){return x*2} minifies to nothing. Turn ES module off for classic scripts.
  • Code that reads names at runtime. fn.name, constructor.name and some dependency injection libraries see the mangled name. Turn off Mangle names if you rely on them.
  • TypeScript or JSX input. Terser stops with a syntax error at the first type annotation or tag.

Example

Input:

// Add tax to a price
export function withTax(price, rate = 0.2) {
  const tax = price * rate;
  return Math.round((price + tax) * 100) / 100;
}

Output (mangle, compress, ES module):

export function withTax(t,n=.2){const o=t*n;return Math.round(100*(t+o))/100}

The comment is gone, the local names are one letter long, 0.2 became .2, and the exported name stays the same so other modules can still import it.

Syntax errors with line and column

Terser has to parse your code before it can minify it. If the code has a syntax error, the tool shows the message with the line and column, for example Line 3, column 14: Unexpected token, so you can jump straight to the problem.

Options

  • Mangle names: shorten local variable, parameter and function names.
  • Compress: apply safe code transforms such as dead-code removal and constant folding.
  • Keep licence comments: keep /*!, @license and @preserve comments.
  • ES module: allow import and export and treat the code as strict.

When to minify by hand

For a full project, let your bundler minify as part of the build. This tool is handy for small jobs: an inline script for a landing page, a bookmarklet, a snippet for a tag manager, or checking how much a function would cost in bytes. To read minified code instead, use the JavaScript beautifier.

Related Tools

Frequently Asked Questions

Which minifier does this use?
Terser. It is webpack's default minifier and an option in Vite and Rollup. It parses modern JavaScript (classes, async/await, optional chaining, ES modules) but not TypeScript or JSX, so compile those to plain JavaScript first.
What is the difference between mangle and compress?
Mangle renames local variables and parameters, so price becomes t. Compress rewrites code into smaller equivalent forms: it drops unreachable code and unused functions, joins var declarations and folds constants, so 60 * 60 * 24 becomes 86400.
Why does the gzip size matter?
Servers normally send JavaScript compressed with gzip or Brotli, so the gzip figure is close to what visitors download. Gzip already handles repeated whitespace well, so the saving from minifying looks smaller once both versions are compressed. Minifying still wins on things gzip cannot remove, such as comments, long local names and dead code.
What does the ES module option do?
It tells Terser the code is an ES module: import and export are allowed, strict mode applies, and top-level names are treated as private. That means non-exported top-level functions get renamed, and if nothing in the file calls them, removed entirely. Turn it off for classic <script> files whose top-level functions other scripts or onclick attributes call.
Which comments are kept with Keep licence comments?
Comments that start with /*! or contain @license or @preserve, which is the convention open-source licences use. A plain /* Copyright ... */ comment is still removed, so add the ! if you need it kept.
Is my code sent anywhere?
No. Terser runs in your browser, so your code stays on your machine.