Skip to content
Everyday Utilities Free • No signup • Instant results

JavaScript Minifier

Minify JavaScript code by removing whitespace and comments.

About the JavaScript Minifier

The JavaScript Minifier compresses JavaScript source code by removing comments, whitespace, and unnecessary characters while preserving functionality. For a typical application bundle, minification saves 40-60% of the file size — more than HTML or CSS minification because JavaScript is denser with syntax (semicolons, braces, parentheses) and more whitespace-sensitive.

JavaScript minification is more delicate than HTML or CSS minification. You cannot simply strip all whitespace because some whitespace is semantically significant: return value is different from return value (JavaScript's automatic semicolon insertion treats the newline after return as a statement terminator). String literals and regex literals can contain characters that look like JS syntax. A naive regex-based minifier will break on these cases.

This tool protects string literals and regex literals by extracting them before minification and restoring them afterwards. Comments (both // line comments and /* */ block comments) are removed. Whitespace around punctuation is collapsed. The result is a single-line minified script that runs identically to the original. For production-grade minification (with mangling of variable names, dead code elimination, and ES6+ transpilation), use Terser, esbuild, or SWC.

How It Works

The minification runs in stages:

  1. Line comment removal: /\/\/.*$/gm matches // comments to end of line and removes them.
  2. Block comment removal: /\/\*[\s\S]*?\*\//g matches /* ... */ comments and removes them.
  3. String extraction: /(['"`])(?:[^\\`'"\\]|\\.)*\1/g matches single-quoted, double-quoted, and template literals, extracts them to an array, and replaces them with placeholder tokens. This protects their content from being mangled by later regexes.
  4. Regex extraction: /\/(?:[^\\\/]|\\.)+\/[gimsuy]*/g matches regex literals and extracts them similarly.
  5. Whitespace collapse: \s+ becomes a single space.
  6. Punctuation cleanup: \s*([{}();,:=<>+\-*/%&|!?])\s* removes whitespace around JS punctuation.
  7. Semicolon removal: ;\s*} becomes } (removes the optional semicolon before a closing brace).
  8. Trim and restore: Leading/trailing whitespace is removed, and the extracted strings and regexes are restored.

Worked Examples

The default input is a small greet function with comments. Minifying produces function greet(name){const defaultName='World';const finalName=name||defaultName;console.log('Hello, '+finalName+'!');return true}greet('HT99') — a single line, no comments, no unnecessary whitespace. The string literals 'World', 'Hello, ', '!', and 'HT99' are preserved exactly because they were extracted before whitespace processing.

For a real-world application bundle like React (about 130KB unminified), production minification with Terser reduces it to about 40KB — a 70% reduction. Combined with gzip, the transferred size is around 13KB. Without minification, the gzipped size would be about 45KB, so minification is still essential.

If your code uses regex literals like /\d+/g or /^[a-z]+$/i, the tool extracts them so the regex syntax is not mangled by whitespace collapse. Without this protection, a regex like /\s +/g would have its internal space collapsed, breaking the pattern.

When to Use This Tool

  • Compressing JavaScript bundles for production deployment.
  • Reducing the size of browser-side libraries and widgets.
  • Preparing JavaScript for embedding in HTML <script> tags.
  • Minifying JavaScript snippets for use in browser extensions or bookmarklets.
  • Compressing code before pasting into size-limited contexts (CMS rich-text editors, chat).
  • Reducing bandwidth costs on high-traffic single-page applications.
  • Pre-minifying third-party libraries before bundling.

Limitations & Disclaimer

This tool performs regex-based minification and does not parse JavaScript into an AST. It may break on edge cases involving automatic semicolon insertion (ASI), regex literals that contain quotes or whitespace, template literals with embedded expressions, JSX, or TypeScript syntax. It does not perform variable name mangling, dead code elimination, or ES6+ feature transpilation. For production-grade minification, use Terser, esbuild, SWC, or the built-in minifier in your bundler (Webpack, Vite, Rollup). Always test minified code before deploying. See our disclaimer for full terms.

Frequently Asked Questions

Will minification break my JavaScript?

Usually no, but there are edge cases. JavaScript's automatic semicolon insertion (ASI) means that <code>return value</code> is interpreted as <code>return; value;</code>. If the tool collapses the newline to a space, the meaning changes. This tool preserves required whitespace, but for safety, always run minified code through tests before deploying. Tools like Terser handle ASI correctly via proper parsing.

Why not just use Terser or esbuild?

You should, for production. Those tools use a proper JavaScript parser (AST-based) and handle edge cases correctly, plus they perform additional optimizations like variable name mangling and dead code elimination. This tool is for quick, in-browser minification when you do not have a build pipeline available &mdash; for example, when pasting a snippet into a CMS or a chat message.

Does this tool mangle variable names?

No. The tool removes only comments and whitespace; it does not rename variables. Mangling (renaming <code>longDescriptiveName</code> to <code>a</code>) saves additional space but requires proper AST analysis to avoid breaking scope. Use Terser for mangling.

Does this tool support modern JavaScript (ES6+)?

Partially. The tool processes any text, but its regex-based string extraction may not handle all ES6+ features perfectly (template literals with embedded expressions, tagged templates, optional chaining with unusual whitespace). For ES6+ code, use Terser or esbuild.

What about minifying TypeScript?

This tool minifies JavaScript, not TypeScript. TypeScript must be transpiled to JavaScript first (using the TypeScript compiler or Babel), then the resulting JavaScript can be minified. Most modern build tools (Vite, Webpack, esbuild) handle both steps automatically.

Is my code uploaded anywhere?

No. Minification happens entirely in your browser using JavaScript string operations. Your code &mdash; including any proprietary logic or sensitive configuration &mdash; never leaves your device.

Last updated: July 21, 2026  ·  Author: HT99 Tools Editorial Team  ·  Reviewed by: HT99 Tools Editorial Team