Skip to content
Developer

JS Minifier

Minify JavaScript by removing comments and unnecessary whitespace.

javascriptjsminifycompressoptimize
Loading tool…

About the JS Minifier

The JS Minifier shrinks JavaScript without changing what it means, using a tokenizer that understands the language well enough to tell division from a regex literal — const ratio = 4 / 2 keeps its division, while const pattern = /[a-z]+/gi survives intact. String, template, and regex literals are preserved byte for byte, including template interpolations, so no data is ever altered. Three switches drive the compression: Strip comments removes // and /* */ comments, Collapse whitespace joins tokens where it is safe, and Drop ; before } removes the last semicolon of a block. Crucially, newlines after return, throw, yield, break, and continue are preserved so automatic semicolon insertion can never silently change control flow. An honest banner sets expectations: this is a basic minifier with no variable renaming or dead-code elimination — for production you should still run terser or esbuild — but for quick compression and size checks it is immediate. Stats show original/minified bytes and savings, and the output downloads as script.min.js.

JS Minifier examples

Input
const ratio = 4 / 2;
const pattern = /[a-z]+/gi;
const lines = text.split(/
/);
Output
const ratio=4/2;const pattern=/[a-z]+/gi;const lines=text.split(/
/);
Note: Division stays division and regex literals stay untouched — the tokenizer decides from context what a / begins.
Input
function add(a, b) {
  return a + b; // sum
}
const total = add(2, 3);
Output
function add(a,b){return a+b}const total=add(2,3);
Note: Comments and whitespace collapse, the ; before } drops, and the return statement keeps its semantics.
Input
const greeting = "Hello, world!";
const pattern = /\d+/g;
Output
const greeting="Hello, world!";const pattern=/\d+/g;
Note: String and regex literals are preserved character-for-character — only the spacing between statements changes.

How to use the JS Minifier

  1. 1

    Paste your JavaScript into the JavaScript input textarea, or click Load sample.

  2. 2

    Set the switches: Strip comments, Collapse whitespace, Drop ; before }.

  3. 3

    Read the informational banner — this is a basic minifier, not a full optimizer.

  4. 4

    Check the Original, Minified, Saved, and Savings stats for the size change.

  5. 5

    Download the result as script.min.js.

Common use cases

  • Quickly shrinking a small script for a bookmarklet or user script.
  • Estimating payload savings before wiring up a production minifier.
  • Comparing your hand-minified output against a reference implementation.
  • Preparing compact inline script content for a template or email.
  • Checking that a regex-heavy script survives a whitespace-only minifier.
  • Learning how tokenizers distinguish regex literals from division.

Best practices

  • Do not ship this output to production — there is no renaming or tree shaking; run terser or esbuild afterward.
  • Verify the minified result in a browser or with node before relying on it anywhere.
  • Keep the readable source in version control and treat the minified file as a build artifact.
  • Watch for ASI-sensitive code: the tool preserves newlines after return/throw/yield/break/continue, but avoid relying on tricky ASI patterns.
  • Spot-check template literals with interpolation after minifying — they are preserved verbatim by design.
  • The stats measure bytes, not performance; minification mainly cuts transfer size, not runtime speed.

Tips

  • Load the sample to see comments, a regex, a template, and arrow functions minify together.
  • Toggle Collapse whitespace off to isolate what comment stripping alone saves.
  • Run the output through node --check in your terminal for a fast syntax sanity check.
  • Use the Savings stat to argue for a proper build step in a project that still ships unminified scripts.

Frequently asked questions

It looks at the previous significant token. After keywords like return, if, or for, after opening punctuation, or after operators, a / starts a regex literal; after a value, identifier, or closing bracket, it is division. That is why both constructs in the sample survive correctly.

No. This is a whitespace and comment remover, exactly as the banner states. Variable renaming, inlining, and dead-code elimination require full parsing and are left to tools like terser or esbuild.

A newline after return, throw, yield, break, or continue triggers automatic semicolon insertion in JavaScript. If the newline were removed, return followed by a value on the next line would change meaning, so the minifier preserves those specific line breaks.

Line comments are kept with their newline restored so the following code does not merge into the comment, and block comments are kept inline. Whitespace is still collapsed around them where safe.

Yes — templates, including nested templates and interpolation expressions, are consumed as opaque tokens and written out verbatim. Their content is never touched, so strings with spaces or braces inside stay intact.

Spaces are kept only where they prevent two tokens from merging into a different token — between two word-like tokens, for instance. Those single spaces are semantically required, which is why the output is still valid JavaScript.

Explore more developer tools

Browse the full collection of developer tools on the hub, or jump back to all categories.

Related tools

Did this tool help?

Anonymous — one tap, no sign-up.