JavaScript minifier — compress JS with Terser in your browser

Paste JavaScript and Terser minifies it in your browser, renaming local variables, dropping dead code and comments, and showing the size before and after, including gzip.

Live demo · real result from this tool
Only safe when nothing outside this file uses them.

Minified code

const prices={apple:.5,bread:2.25,milk:1.1};function total(e,t){let o=0;for(const[t,r]of Object.entries(e))o+=prices[t]*r;return"SAVE10"===t&&(o*=.9),Math.round(100*o)/100}console.log(total({apple:4,bread:1},"SAVE10"));
Saved29.4%
Original
310 B
Minified
219 B
Minified + gzip
192 B

Processed on your device — nothing is uploaded.

How to use the JavaScript minifier

  1. Paste your code into the JavaScript box.
  2. Keep Shorten local variable names and Compress (remove dead code, fold constants) ticked for the smallest output.
  3. Tick Also rename top-level names only if nothing outside this file uses its functions or variables.
  4. Leave **Keep /! licence / comments on if the code carries a licence notice.
  5. Copy the Minified code or download script.min.js, and check the Saved, Original, Minified and Minified + gzip sizes.

What it does and when to use it

Minifying makes JavaScript smaller so pages load faster. This tool goes beyond removing spaces and comments. It renames local variables to one or two letters, removes code that can never run, folds constant expressions, and rewrites some statements in shorter equivalent forms. Use it for a small site without a build step, a bookmarklet, a script you embed in a CMS, or to see how much a file could shrink.

For projects that change often, a bundler with Terser or esbuild built in does this automatically on every build. To read minified code, run it through the JavaScript formatter. For stylesheets there is the CSS minifier, and to make code deliberately harder to follow, the JavaScript obfuscator adds string escaping on top of renaming.

How it works

The page loads Terser, an open-source minifier, the first time you use it. Terser parses your code into a syntax tree, so it knows which names are local and which strings are strings. With Compress on it makes two optimisation passes. With Shorten local variable names on it renames variables inside functions, which is safe because nothing outside a function can see them. Finally it prints the tree with no unneeded whitespace. The gzip figure is calculated in the page with the fflate library, to show roughly what a server would send.

Worked examples

These come from the minifier with default settings.

A small function.

function add(first, second) {
  // sum
  return first + second;
}
console.log(add(1, 2));

becomes function add(d,n){return d+n}console.log(add(1,2));. The comment and whitespace are gone and the parameters are renamed, while the top-level name add stays because top-level renaming is off. The tool reports Saved: 42.7% (89 bytes to 51). The gzip size is 69 bytes, larger than the minified code, because the snippet is tiny.

A syntax error. var x = ; returns “Syntax error — line 1, column 9: Unexpected token: punc (;)” with a marker under the semicolon.

Limits and tips

  • Minify from the original source. Running minified code through again gains little and makes debugging harder.
  • No source map is produced here. Use a build tool if you need one.
  • Code that reads function names, such as fn.name or some dependency-injection setups, can break when names are shortened. Turn off renaming if that applies.
  • Modern syntax such as classes, async and modules is supported and kept; the tool does not convert code for old browsers.

Frequently asked questions

Is minified code safe to use in production?
Terser is a widely used minifier and parses your code before changing it, so the result should behave the same. Still, test the minified file before you deploy it, especially with "Also rename top-level names" turned on.
What does "Also rename top-level names" do?
It shortens names declared at the top of the file as well as local ones. That saves more bytes, but breaks any other script or HTML attribute that calls those functions by name. Leave it off unless the file is fully self-contained.
Why is the gzip size larger than the minified size?
For very small snippets, gzip adds a fixed header and its savings cannot cover it. On real files of a few kilobytes or more, gzip usually shrinks minified code a lot further.
Can minified code be turned back into the original?
Not fully. A formatter can restore line breaks and indentation, but shortened names and removed comments are gone. Keep your original source, and use source maps from a build tool if you need to debug production code.
Does this protect my code from being copied?
No. Minified code is harder to read but anyone can format it and follow the logic. Never put secrets such as API keys in front-end JavaScript.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close