JavaScript minifier
Strip comments and whitespace from JavaScript without renaming anything. Deliberately conservative: it will not shorten your variable names, and it will not break your code.
What this minifier does — and what it refuses to do
There are two kinds of JavaScript minifier. One parses your code into a full syntax tree, renames every local variable to a single letter, folds constants and drops unreachable branches. That is what Terser and esbuild do, and it is what you want in a real build pipeline.
This is the other kind: a careful whitespace and comment stripper. It removes line comments, block comments, indentation and blank lines, puts statements back together where that is provably safe, and stops there. Your identifiers keep their names. The result is typically 20 to 40 percent smaller than the original, where a full compiler might reach 60.
That trade is on purpose. A tool that renames variables has to understand your entire program — scopes, closures, eval, with, dynamic property access — and when it gets that wrong you get a bug that only shows up in production. A tool that only removes whitespace cannot make that mistake.
The parts that are easy to get wrong
- Automatic semicolon insertion. Removing a newline can change what your program means.
returnfollowed by a newline returnsundefined; join those lines and you return the next expression instead. This minifier only joins two lines when the first ends, or the second starts, with a character that makes the join unambiguous. - Template literals. The text inside backticks is data, including its newlines and anything that looks like a comment. It is captured whole and put back untouched.
- Regular expressions. A
/can start a regex or be a division sign, and the only way to tell is to look at what came before it. Get it wrong and you either eat half the file as a regex or treat a regex body as code. - Strings containing code-like text.
"a // b /* c */"is a string, not a comment. - Subtraction of a negative.
x - -1cannot becomex--1, so spaces around+and-are always left alone.
When to use something heavier
If you ship an application bundle, use a real build tool: esbuild, Terser or whatever your bundler already includes. They mangle names, tree-shake unused exports and produce source maps. Use this page for the cases where that is overkill: a single script you paste into a page, a snippet for a client, a widget you hand to someone else, or a quick check of how much whitespace a file is carrying.
Does it rename my variables?
No, never. Identifiers are left exactly as they are. That is why the saving is smaller than a full compiler's, and why the output cannot break on a scoping edge case.
Will the output still work in older browsers?
It runs wherever your original ran. Minifying is not transpiling: no syntax is converted, so arrow functions stay arrow functions and let stays let. If you need old-browser support, run Babel or esbuild first.
Can I minify a module with import and export?
Yes. import and export statements are just code as far as this tool is concerned, so they pass through with their whitespace cleaned up.
Does it produce a source map?
No. Source maps need a full parse of the original and a record of every position change. If you need debugging support in production, use a bundler.
Other tools on this site
Read more
- What is CSS minification? – What a minifier removes and what it saves
- CSS optimization for speed – Beyond minification: critical CSS and Core Web Vitals
- What breaks a CSS minifier? – Live test of the constructs minifiers get wrong
- Minifying CSS in WordPress – Which plugin does what, and when it breaks
- Frequently asked questions – Answers to the questions we get most
- Useful links – Reference material on CSS and speed
- About this site – Who makes this and how to reach us