JavaScript Minifier
Minify JavaScript by removing whitespace, comments, and unnecessary semicolons.
About the JavaScript Minifier
The JavaScript Minifier on HT99 Tools strips // line comments, /* */ block comments, and collapses whitespace — while preserving string literals, regex literals, and the comment-marker /*! ... */ used by tools such as jQuery's licence header. Typical savings are 20–50% on hand-written code, less on code that has already been processed by a build step.
JavaScript minification is harder than HTML or CSS minification because the language has multiple lexical contexts that look similar. A // inside a string (var url = 'https://example.com') is not a comment. A /* */ inside a regex (var re = /\/\*/) is not a comment either. A naive regex that strips everything between // and a newline will break URL literals; a regex that strips everything between /* and */ will break regex literals that contain those characters.
This tool walks the source character by character, identifying whether each / begins a comment, a regex literal, or a division operator. The tokeniser is not a full ECMAScript parser — it cannot distinguish a regex from a division in every ambiguous case (that requires the previous token, as formalised in the ECMAScript spec §11.8.5) — but it handles the common cases correctly.
How It Works
The minifier runs a single-pass tokeniser that recognises five token types: string literals (single, double, and backtick), // line comments, /* */ block comments, regex literals (heuristic), and other characters. Strings are matched by walking forward from the opening quote until the matching closing quote, accounting for backslash escapes. Regex literals are matched heuristically as /... followed by ASCII word characters.
Comment stripping skips line-comment tokens (when enabled) and block-comment tokens (when enabled), except for block comments that begin with /*! — the convention used by jQuery, Bootstrap, and many libraries to mark licence headers that must be preserved. The licence header for jQuery is roughly 700 bytes; stripping it would violate the MIT licence terms.
Whitespace collapsing runs after the tokeniser: every run of whitespace becomes a single space, spaces around operators and punctuation are removed, and a semicolon immediately before } is removed. The tool does not rename variables (a job for Terser), does not convert ES6+ to ES5 (a job for Babel or SWC), and does not dead-code-eliminate unused branches — those require an abstract syntax tree.
Worked Examples
The default input is 290 bytes of commented JavaScript: a sumArray function with two line comments and a CommonJS export guard. With all three options enabled, the output is roughly 95 bytes — a 67% reduction. Both line comments are removed, the function body collapses to one line, and the trailing ; before } is dropped.
Concretely, the output becomes function sumArray(arr){const total=arr.reduce((acc,n)=>acc+n,0);return total}if(typeof module!=='undefined'){module.exports=sumArray}. The arrow function (acc, n) => acc + n keeps the spaces around => because removing them would change the meaning of = and > tokens.
A pathological case the tool handles correctly: var url = 'https://example.com'. The // inside the string is detected by the tokeniser as part of the string literal, not a comment, and is preserved. A regex-only minifier would have stripped everything from // to end of line and broken the code.
When to Use This Tool
- Quickly trimming a small utility script before pasting it into a browser console.
- Removing author comments from a code snippet before sharing.
- Producing a compact single-file build for a small library or widget.
- Reducing the size of a Node.js script before deploying to a serverless function.
- Stripping licence headers is intentionally not done (
/*! ... */is preserved). - Compressing a bookmarklet (where every byte counts against URL length limits).
- Demonstrating the size of a dependency tree by minifying its concatenation.
Limitations & Disclaimer
This tool minifies JavaScript using a hand-written tokeniser, not an ECMAScript-compliant parser. It does not rename variables, eliminate dead code, transpile ES6+ syntax, or hoist functions. The tokeniser's regex-literal detection is heuristic and may misclassify certain edge cases (a regex literal that begins with a space, a division immediately following a return statement). ASI edge cases (where minification can join two lines and change semantics) are not handled. For production builds, use Terser, esbuild, SWC, or your framework's asset pipeline. See our disclaimer for full terms.
Frequently Asked Questions
Is this minifier safe for production code?
For small scripts and snippets, yes. For production applications, prefer a real minifier such as Terser, esbuild, SWC, or the asset pipeline of your framework (Vite, Webpack, Rollup). Those tools build an abstract syntax tree and can rename variables, eliminate dead code, and convert ES6+ to ES5 — transformations that produce much smaller output than regex-based minification.
Why is regex-based JS minification risky?
Because the lexical structure of JavaScript is context-sensitive: a slash can be a division, a regex literal, or a comment marker, depending on the previous token. The ECMAScript spec formalises this in §11.8.5, and a correct implementation requires the previous meaningful token. Regex-based minifiers cannot reliably distinguish the three cases in every situation; this tool handles the common cases but is not spec-perfect.
Does the tool rename variables?
No. Variable renaming (also called mangling) is the single biggest source of size reduction in modern minifiers — Terser typically saves another 30–50% on top of whitespace removal by renaming local variables to short names like <code>a</code>, <code>b</code>, <code>c</code>. This tool does not do that because it requires scope analysis (which variables are local, which are exported, which are reserved).
Why are some block comments preserved?
Block comments that begin with <code>/*!</code> are preserved by convention. This is the marker popularised by jQuery and adopted by Bootstrap, Lodash, and many other libraries to indicate licence or copyright information that must be preserved. Removing such a header would violate the MIT, BSD, or Apache licence terms.
Will minification break my code?
It can, in three known edge cases: ASI (automatic semicolon insertion) cases where a statement's leading <code>(</code> or <code>[</code> would merge with the previous line; regex literals that the heuristic misclassifies as comments or divisions; and template literals containing <code>${...}</code> expressions with newlines. If your minified code breaks, disable whitespace collapsing and use a real minifier.
Is my code uploaded anywhere?
No. Minification runs entirely in the browser. Your source code, including comments and proprietary logic, never leaves the device.
Last updated: September 9, 2026 · Author: HT99 Tools Editorial Team