JS Minifier

JS Compressor. Minify JavaScript to reduce file size. Free JS minifier.

With the first two off, only spaces, line breaks and comments are removed. That is the safest setting.

Rate this tool

How useful was this tool?

JS Minifier

Paste your JavaScript, open a .js file, or load the example, then click Minify Code.

By default the tool only removes comments, spaces and line breaks, which never changes how code behaves. Two optional settings shrink the file further by rewriting the code, and both are marked with a warning triangle.

The result shows the original and minified sizes, plus both sizes after gzip and Brotli, which is closer to what visitors actually download. Copy the result, download it as a .min.js file, or download a source map along with it.

Minification runs in your browser, so your code is never uploaded.

What minifying actually does

Here's a small function before minifying (356 bytes):

// Calculate the total price of the items in a cart
function cartTotal(items, taxRate) {
  const DEBUG = false;
  let subtotal = 0;

  for (const item of items) {
    subtotal += item.price * item.quantity;
  }

  if (DEBUG) {
    console.log('Subtotal:', subtotal);
  }

  const total = subtotal * (1 + taxRate);
  return Math.round(total * 100) / 100;
}

With the default settings (237 bytes, a third smaller):

function cartTotal(items,taxRate){const DEBUG=false;let subtotal=0;for(const item of items){subtotal+=item.price*item.quantity}if(DEBUG){console.log("Subtotal:",subtotal)}const total=subtotal*(1+taxRate);return Math.round(total*100)/100}

Only the comment and the formatting are gone. Every name and every line of logic is untouched.

With Shorten Variable Names and Optimize Code on (116 bytes, two-thirds smaller):

function cartTotal(t,n){let o=0;for(const n of t)o+=n.price*n.quantity;const r=o*(1+n);return Math.round(100*r)/100}

This time the code itself was rewritten:

  • Local names are shortened. items became t, subtotal became o, and so on. The minifier even reuses a letter when two variables never exist at the same time, which is why n appears twice.
  • Dead code is removed. DEBUG is always false, so the if (DEBUG) block could never run. The minifier deleted it, along with the variable.
  • Small rewrites. Unneeded braces are dropped, and total * 100 became 100*r, which compresses slightly better.
  • cartTotal keeps its name. Anything the rest of your site can use keeps its name, even if nothing in this file calls it, because another script might.

How much it saves on a real library

Here's jQuery 3.7.1 at each stage:

Stage File size After gzip After Brotli
Original jquery.js 278.6 KB 81.7 KB 67.9 KB
Default: comments and whitespace removed 135.7 KB 38.5 KB 33.9 KB
+ Shorten Variable Names 89.8 KB 30.2 KB 27.3 KB
+ Optimize Code 84.7 KB 29.5 KB 26.8 KB

 

The safe default does most of the work. It halves the file. The two optional settings remove another 51 KB of raw size, but only about 9 KB after gzip and 7 KB after Brotli, because compression already handles long, repeated names well. If you'd rather not test a rewritten file, the default alone is a perfectly good result.

Minification and server compression work best together. Gzip alone gets jQuery down to 81.7 KB, and minification alone to 84.7 KB. Both together get it to 29.5 KB, about a third of either on its own.

Brotli beats gzip at every stage, by roughly 10 to 20 percent here. Most modern servers and CDNs can serve Brotli to browsers that support it, which today is nearly all of them. If you're not sure what your server sends, check with the GZIP Compression Test.

The options

Shorten Variable Names and Optimize Code are off by default and marked with a warning triangle, because they rewrite your code. Turn either one on and the tool reminds you to test the result before using it.

Shorten Variable Names. Renames variables and parameters inside functions to one or two letters. Functions and variables that the rest of your site uses keep their names.

Optimize Code. Removes code that can never run and unused variables inside functions, and simplifies expressions. Top-level functions stay, even if nothing in this file calls them.

Keep License Comments. Keeps comments that start with /*! or contain @license. Most open-source licenses, including MIT and BSD, require the copyright notice to stay with the code. Leave this on when you minify someone else's library.

Remove Console Calls. Strips console.log and every other console method, including console.error and console.warn. It works on its own, whatever the other settings are. If you rely on console messages for error reporting in production, leave it off. And if an argument does real work, like console.log(count++), that work disappears along with the call.

ES modules. If your code uses import, export or top-level await, the tool treats it as a module. Module code is private to its file, so top-level names can be shortened safely too. The notice under the result tells you when this happens.

When minified code can break

With the default settings, it can't. Removing comments and whitespace doesn't change what code does.

The two optional settings are safe for ordinary code too. Global functions and variables, and anything a module exports, keep their names. Only names inside functions are shortened, and only code that can never run is removed.

What can break is code that depends on the names inside it. That means code that reads the names of its own functions, classes or parameters. The classic case is AngularJS 1.x, which worked out what to inject from parameter names: minified, function($scope, $http) becomes function(t, n), and the app stops working. The fix is the framework's explicit annotation syntax, or leaving Shorten Variable Names off.

Whenever you use the optional settings, test the minified file on your site before you replace the original, and keep the original so you can go back.

Tips

Keep your original file. Edit the readable version and minify again; never edit the .min.js. Minified code can be reformatted, but the original names and comments are gone for good.

Download the source map too. A minified file is one long line, so browser errors normally point to something like "line 1, column 48213." Upload the .map file to the same folder as your .min.js, and the browser's developer tools will show errors at the right place in your original code.

Remember what's in a source map. It usually contains your complete original code, comments included, and anyone who finds the .map file on your server can read it. That's fine for most sites. If you'd rather not publish your original source, keep the map on your own computer and use it only when you're debugging.

Fix syntax errors first. If the code has a syntax error, the tool shows the exact line and column instead of producing broken output. That position refers to your original code.

Using a build tool? Vite, webpack, esbuild and similar tools already minify on production builds. This tool is for everything outside a build setup: theme and plugin files, scripts on small or older sites, snippets for tag managers, or a quick check of how small a file can get.

FAQ

Can I turn minified code back into the original?

Only partly. The JS Formatter restores line breaks and indentation, but shortened names and deleted comments can't be recovered from the minified file alone. A source map can map positions back to your original code, which is why it's worth keeping.

Does minifying protect my code from being copied?

No. Anyone can reformat minified code and read the logic. If you want code that's genuinely harder to follow, that's what the JS Obfuscator does, though obfuscated code is usually bigger and slower than minified code. Don't publish a source map for code you want to protect, since the map contains the original.

Does minifying JavaScript help SEO?

Indirectly. Smaller files download faster, which helps page speed, especially on mobile connections. But browsers also have to parse and run your JavaScript, and minifying doesn't reduce that work much. If a page feels slow because of scripts, removing JavaScript you don't need usually helps far more than squeezing the rest.

Is my code uploaded anywhere?

No. Everything happens in your browser, and we never see or store your code.

Minifying stylesheets and markup too? Use the CSS Minifier and HTML Minifier.

Note: All processing is done client-side. We do not store or see your data.

Contact

Missing something?

Feel free to request missing tools or give some feedback using our contact form.

Contact Us