JavaScript Minifier
Paste JavaScript code and get a minified version instantly. Strips comments and whitespace to reduce bundle size. Everything runs in your browser β no upload.
Minified JavaScript appears hereβ¦Safe Minification β Comments & Whitespace Only
This tool strips comments and excess whitespace. It does not rename variables, inline functions, or perform dead-code elimination. For production bundle optimization, use Terser, esbuild, or your build pipeline's minification step.
Comments removed
// single-line and /* block */ comments
Whitespace collapsed
Tabs, newlines, indentation
Faster parse time
Less JS for the browser to compile
How the JavaScript Minifier Works
1. What This Tool Actually Does
This minifier runs six passes over your JavaScript source, in order. It first removes any line that is entirely a // comment, then removes trailing // comments that follow code β but only when the // is directly preceded by a semicolon, brace, comma, parenthesis, or whitespace, which avoids mistaking a :// inside a URL string for a comment. Next it strips every /* block */ comment. It then collapses every newline plus its following indentation into a single space, and collapses any remaining runs of multiple spaces into one. It removes spaces immediately touching operator and punctuation characters (= + - * / % & | ^ ! < > , ; : { } ( ) [ ]) where doing so is always safe. Finally, as a safety net, it scans for reserved keywords like return, typeof, or function that ended up sitting directly against the next character, and re-inserts a single space so a keyword never accidentally fuses with an identifier.
2. How to Use the JS Minifier
- Paste your JavaScript into the Input JavaScript panel, or click Load Sample JS to see it run on a working performance-tracking snippet.
- Watch the Minified Output panel update live as you type, entirely in your browser.
- Check the green stats pill for exact input/output byte counts and the percentage saved for your file.
- Click Copy and paste the result wherever it's needed β an inline
<script>tag, a WordPresswp_add_inline_script()call, or a small standalone utility file. - Always spot-check the output against the original, especially for code with unusual comment placement or string content β read the limitations below before shipping to production.
3. Why This Is "Safe" Minification, Not Full Compilation
JavaScript engines tokenize source code into keywords, identifiers, operators, and literals β whitespace between tokens is almost always insignificant to the parser, and comments are discarded before execution ever begins. That's why removing them doesn't change what the code does. But JavaScript has real edge cases where whitespace does matter: inside string and template literals, inside regular expression literals, and around Automatic Semicolon Insertion (ASI), where a line break can silently terminate a statement even without an explicit semicolon.
Because this tool works on raw text with regular expressions rather than parsing a real Abstract Syntax Tree (AST), it takes a conservative approach: it never deletes newlines outright (it replaces them with a single space, preserving ASI-relevant statement breaks), and it never touches whitespace inside quotes since it doesn't track whether it's currently "inside a string" the way a real parser would. Tools like Terser or esbuild build a full AST and can therefore go further β renaming local variables, dropping unreachable branches, and inlining constants β none of which this tool attempts.
4. A Real Example
Take this small function with a full-line comment:
function add(a, b) {
// sum two numbers
return a + b;
}Running it through the minifier produces:
function add(a,b){return a+b;}The comment line is gone entirely, the space inside (a, b) after the comma is removed, and every space around { } + ; collapses β while the space between return and a is preserved, since it's a plain identifier-to-identifier boundary the tool never touches. Load this exact example with the Load Sample JS button to see a longer version with a full IIFE and JSDoc block.
5. Practical Use Cases
- Inline WordPress scripts: Trimming a small tracking or analytics snippet before pasting it into an inline
<script>block or a theme'sfunctions.phpviawp_add_inline_script(), where every byte is added to every page load. - Standalone utility scripts: A small, hand-written helper script that isn't part of a bundler pipeline can still be compacted before deployment.
- Quick weight audits: Paste an existing script to see how much of its footprint is comments and formatting versus actual logic.
- Reducing HTML payload: Inline scripts embedded directly in HTML (rather than external files) count toward the initial document's transfer size β minifying them shrinks that specific payload.
6. Common Mistakes & Limitations
- Not AST-based: This tool never renames variables, removes dead code, or inlines expressions. For production bundle optimization, pair it with a real build step using Terser or esbuild.
- Regex comment detection has edge cases: A standalone line containing only a protocol-relative URL, or a comment-like sequence inside a template literal, could theoretically be mishandled since the tool doesn't track string/template boundaries. Review unusual code manually.
- ASI-dependent code is inherently risky: Code that deliberately omits semicolons and relies on line breaks to separate statements is fragile under any minifier, including this one. Prefer explicit semicolons in source code you plan to minify.
- Not a replacement for tree-shaking: Unused functions, imports, or variables are left in place exactly as written β this tool only removes formatting, not logic.
Frequently Asked Questions
What does JavaScript minification do?
JS minification removes comments, whitespace, and newlines from JavaScript source code to produce a compact version. It reduces the file size browsers must download, parse, and compile β directly improving Time to Interactive (TTI) and Total Blocking Time (TBT).
Does this tool rename variables or perform tree-shaking?
No. This tool performs safe whitespace and comment stripping only. It does not rename variables, remove dead code, or perform AST-level transformations. For production bundles that require advanced optimization, use build tools like Terser, esbuild, or Rollup.
Does my JavaScript code get uploaded?
No. All processing happens in your browser using JavaScript string manipulation. Your code never leaves your machine.
How much does JS minification help Core Web Vitals?
JavaScript parsing and execution are major contributors to Total Blocking Time (TBT) and Interaction to Next Paint (INP). Smaller JS bundles mean less parse time on the main thread. Combined with async/defer loading, minification can meaningfully improve both TBT and INP scores.
Is it safe to minify code that omits semicolons (relies on ASI)?
Use caution. This tool collapses newlines into single spaces rather than deleting them, which preserves the line break that JavaScript's Automatic Semicolon Insertion (ASI) relies on in most cases. However, code that intentionally omits semicolons between statements is inherently fragile under any line-joining transformation. If your source relies on ASI, add explicit semicolons before minifying, or use an AST-based tool like Terser that understands statement boundaries.
Can regex-based comment stripping break my code?
In rare edge cases, yes. Because this tool scans text rather than parsing a real syntax tree, a line consisting only of a protocol-relative URL string like "//cdn.example.com" could theoretically be misread as a comment if it sits alone on its own line. Always diff the minified output against the original for unusual code before deploying to production.
What is the difference between this tool and Terser or esbuild?
Terser and esbuild parse JavaScript into an Abstract Syntax Tree (AST), which lets them safely rename local variables, remove genuinely unreachable code, and merge statements without any risk of breaking string or regex literals. This tool works on raw text with regular expressions, which is fast and dependency-free but limited to safe whitespace-and-comment removal β it cannot perform those deeper, AST-level optimizations.
