How to use the JavaScript minifier
- Paste your code into the JavaScript box.
- Keep Shorten local variable names and Compress (remove dead code, fold constants) ticked for the smallest output.
- Tick Also rename top-level names only if nothing outside this file uses its functions or variables.
- Leave **Keep /! licence / comments on if the code carries a licence notice.
- 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.nameor some dependency-injection setups, can break when names are shortened. Turn off renaming if that applies. - Modern syntax such as classes,
asyncand 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?
What does "Also rename top-level names" do?
Why is the gzip size larger than the minified size?
Can minified code be turned back into the original?
Does this protect my code from being copied?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.