How to use the JavaScript minifier
- Paste JavaScript in the left pane, or drop a
.jsfile on it. - The minified code appears on the right. The status bar shows the size before and after, the percentage saved and the gzip size, for example
558 B → 328 B (−41%) · gzip ≈ 258 Bfor the sample. - Copy the result or download it as
script.min.js.
Minification uses Terser, the minifier behind most JavaScript bundlers, with the same defaults you get from a build tool. If you’d rather make code readable, the JavaScript Formatter does the opposite job.
Options
- Compress rewrites code into shorter equivalents: it removes dead branches and unused code, folds constants (
1 + 2becomes3), joins statements and turnsif (a) b()intoa && b(). It runs two passes. - Mangle names shortens local variable and parameter names to one or two letters. Globals and object properties are never renamed.
- Comments: License only keeps comments that start with
/*!or contain@licenseor@preserve, which is the usual way to keep copyright notices. Remove all drops every comment and Keep all keeps them all. - Output syntax (More options): ES5, ES2015 or ES2020+. Lower targets stop Terser from producing newer syntax.
- ES module (More options): treats the input as a module, so top-level names can be mangled and unused top-level code is removed.
Example
function greet(name) {
const message = "Hello, " + name + "!";
if (false) {
console.log("never runs");
}
return message;
}
console.log(greet("Linh"));
With the default options, this becomes:
function greet(e){return"Hello, "+e+"!"}console.log(greet("Linh"));
The if (false) block and the temporary variable are gone, and name became e. The function name stays because a script’s top-level greet is a global. With ES module on, Terser knows nothing outside the file can call greet, so it inlines the whole thing:
console.log("Hello, Linh!");
The output syntax matters for shorthand properties. const user = { name: name } becomes {name} with ES2020+, but stays {name:name} with ES5.
Errors
Terser has to parse the code before it can minify it, so a syntax error stops minification. The error names the problem and points to the line and column. For example, a missing closing parenthesis in
const total = items.reduce((sum, item) => sum + item.price, 0;
gives Unexpected token punc «;», expected punc «,» at line 1, column 62. Terser reads JavaScript only: TypeScript type annotations and JSX are syntax errors here, so compile them to JavaScript first.
If your code calls eval() or uses with, you get a warning. Terser can’t know which local names the evaluated code refers to, so it leaves names in those scopes unchanged and the result is larger.
Minifying a whole page
To minify the scripts and styles inside an HTML file in one go, use the HTML Minifier, which runs inline <script> blocks through the same Terser settings. For stylesheets on their own, the CSS Formatter has a minify mode.
Equivalent command
The defaults here match this Terser command line:
npx terser input.js --compress passes=2 --mangle --comments some --ecma 2020 -o input.min.js
Add --module for ES modules. In a build tool, these options go into the Terser plugin’s terserOptions.
Frequently asked questions
Is my code uploaded?
No. Terser runs inside your browser, in a background worker, and the code never leaves your device.
Will minification change what my code does?
It shouldn't. Terser only applies transformations that keep the program's behavior, assuming normal JavaScript: no code that reads function names through Function.prototype.name, no reliance on local variable names via eval, and no code that inspects its own source text. If you depend on one of those, turn off Mangle names or Compress.
Why are my top-level functions not renamed?
In a classic script, top-level declarations are globals that other scripts can call, so renaming them would break those callers. Turn on ES module under More options when the file is a module: then top-level names are private, and unused ones are removed.
Does the ES5 setting convert modern syntax to ES5?
No. Output syntax only stops Terser from introducing newer syntax of its own, such as { name } shorthand. Arrow functions, classes and const you wrote stay as they are. To run in old browsers, transpile with Babel or esbuild first, then minify.
What does the gzip size mean?
It is the size of the minified code after gzip compression, measured in your browser with the same deflate algorithm web servers use. That is roughly what a visitor downloads when your server compresses JavaScript, which most do. Brotli usually comes out a little smaller.
Last updated