JavaScript Minifier

Basic JavaScript minifier that removes comments and unnecessary whitespace

About This Tool

JavaScript minification removes characters that don't affect program behavior — comments, optional whitespace, and sometimes semicolons — to shrink file size for faster delivery. Advanced minifiers also rename local variables, fold constants, and eliminate dead code.

This tool performs the safe subset: comment removal and whitespace collapsing. It won't break syntactically valid input, but it also won't match the output size of full minifiers like Terser or esbuild, which apply mangling and dead code elimination.

The basic transformation is regex-friendly. Strip /* ... */ block comments and // line comments. Collapse runs of whitespace to a single space. Remove whitespace around operators and punctuation where the parser doesn't need it. Optionally drop trailing semicolons before closing braces (ASI handles them). The result is typically 20 to 40 percent smaller than source, depending on how heavily commented the code was. Real bundlers do far more: variable name mangling (turning longButDescriptive into a or _b), dead code elimination via tree-shaking and side-effect analysis, constant folding (3 + 4 becomes 7 at build time), inline expansion of small functions, and reordering of statements for compression. Terser and esbuild reach 50 to 70 percent reduction on top of the comment-and-whitespace pass.

A worked example. Input (180 bytes):

function calculateTotal(items) { // sum the prices let total = 0; for (const item of items) { total += item.price; } return total; }

This tool's output (84 bytes): function calculateTotal(items){let total=0;for(const item of items){total+=item.price;}return total;}

Terser's output (47 bytes): function calculateTotal(t){let l=0;for(const o of t)l+=o.price;return l}

The difference is mangling and ASI exploitation, which only Terser-class tools attempt safely.

Limitations to be honest about. Comment-and-whitespace stripping is safe for syntactically valid input. The risk cases: code that uses Function.prototype.toString to read its own source (rare but real, e.g., older AngularJS dependency injection), code that parses comments at runtime (also rare), and templates inside template literals where whitespace is semantically meaningful. Run the minified output through your test suite before shipping. For production, use esbuild, Vite, Rollup, or Webpack — they handle module resolution, tree-shaking, code splitting, and full minification in one pass. This tool is for one-off snippets, inline event handlers, single-file embeds, or environments where running a build pipeline is overkill.

The about text and FAQ on this page were drafted with AI assistance and reviewed by a member of the Coherence Daddy team before publishing. See our Content Policy for editorial standards.

Frequently Asked Questions