JavaScript Minifier
Shrink JavaScript safely, strings, regex and template literals left untouched.
Paste JavaScript to minify it.
This performs conservative, lexer-based minification: comments and redundant whitespace are removed, but nothing is renamed or restructured. A production bundler such as esbuild or Terser will do considerably better because it can shorten local variable names and remove unreachable code, transformations that need a full parser and are not safe to attempt here.
Features
- Removes line and block comments
- Collapses redundant whitespace
- Preserves strings, template literals and regex literals exactly
- Byte savings report
- Never renames identifiers, so behaviour cannot change
How to use it
- Paste your JavaScript into the input box.
- Choose which cleanups to apply.
- Press Minify.
- Copy or download the result.
Why naive JavaScript minification breaks code
Stripping comments with a regular expression is the classic mistake. The sequence // appears inside URLs in strings, and /* can appear inside a regex literal. A naive comment-stripper turns const url = 'https://example.com' into const url = 'https: and breaks the file. This minifier walks the source character by character, tracking whether it is inside a string, template literal, regex or comment, which is the only reliable way to tell them apart.
Distinguishing a regex literal from a division operator is genuinely ambiguous without parsing. In a / b / c the slashes are division; in a = /b/g they delimit a regex. The lexer here uses the standard heuristic of looking at the previous significant token, after an identifier, number or closing bracket a slash is division; otherwise it starts a regex. That is correct for essentially all real code.
Automatic semicolon insertion is why the keep-semicolons option defaults to on. JavaScript will insert semicolons at line breaks under certain rules, so removing newlines from code that relies on ASI changes its meaning. A line starting with ( or [ is the classic hazard, it gets attached to the previous line as a call or index. Keeping semicolons and only collapsing whitespace avoids the whole category of problem.
Frequently asked questions
Related tools
Further reading
Read the full guide on the 123MiniApps blog.