How to use the JavaScript minifier
- 1Paste your script into the JavaScript box, or press Example.
- 2Keep Shorten variable names and Optimize code ticked for the smallest result. Tick
Remove console.* callsfor production builds. - 3Leave Code type on Detect; choose ES module if the file uses
import/exportbut the detection misses it. - 4Press Minify JavaScript. The footer shows the saving and the gzipped sizes; press Copy or Download (
script.min.js).
Example
// Greeting helper
function greet(name) {
if (!name) {
return 'Hello, stranger';
}
const greeting = 'Hello, ' + name + '!';
return greeting;
}
console.log(greet('Ada'));function greet(e){return e?"Hello, "+e+"!":"Hello, stranger"}console.log(greet("Ada"));Saved 52% (182 B → 87 B); gzipped 147 B → 96 B. The parameter name became e and the if/return pair became one conditional expression. greet keeps its name because it is a top-level function another script could call.
Whitespace removal vs mangling
Minification works in three layers, each with its own option:
- Whitespace and comments are always removed. Comments starting with
/*!or containing@licenseare kept when Keep license comments is ticked, because many licenses require the notice to stay with the code. - Shorten variable names (mangling) renames local variables and parameters to one or two letters. Top-level names in a classic script are left alone, since other scripts on the page may use them.
- Optimize code (compression) rewrites code into shorter equivalents: dead branches removed, constant expressions folded,
ifstatements turned into&&or?:. It runs two passes.
If the minified file throws errors that the original didn't, try again with Optimize code off to find out which layer is responsible.
Source maps
A source map lets browser developer tools show the original file, with real names and line numbers, when an error happens in minified code. This page doesn't create one, because a source map is only useful if it is deployed next to the minified file and kept in step with it, which is a build-tool job. Webpack, Vite, esbuild and Rollup all generate source maps with a single setting. Use this tool for one-off scripts, snippets for a CMS or tag manager, and checking how much a file would shrink.
Gzip and Brotli: why transfer size differs
Servers usually compress text before sending it, with gzip or Brotli. Compression already removes much of the repetition that whitespace adds, so the gain from minifying is smaller over the network than on disk. In the example, minifying saved 52% of the raw size but 35% of the gzipped size.
The footer shows both numbers. The gzipped figure (level 9) is close to what visitors download from a server with gzip enabled; Brotli is usually a little smaller still. Minifying and compressing together beat either one alone, because a mangled, comment-free file also compresses better.
When your build tool already minifies
If your site is built with a bundler (Vite, Next.js, webpack, Parcel), production builds are already minified, usually with Terser or esbuild. Minifying that output again gains little. Check before you add a step: open the deployed .js file. If it is one long line with short variable names, it is minified.
WordPress and other CMSs often serve scripts unminified. A caching or optimization plugin can minify them, or you can minify a theme's custom script here and upload the .min.js version. Measure the effect with the website speed test.
Is my text private?
This tool runs entirely in your browser. Your text is not uploaded to our servers.
