JavaScript Minifier

Shrink a script in your browser by stripping comments and collapsing whitespace. It is a naive regex minifier: fast, but it can break real code.

Developer Tools● Free No upload Instant

Loading JavaScript Minifier…

Your browser is preparing the tool. It runs 100% locally.

Quick answer

Paste JavaScript and the tool removes comments and the whitespace around operators and punctuation, collapsing the script toward one line. Be warned: it is a naive regex minifier, not a real one. It does not understand strings, template literals or regular expressions, so it can break working code in those cases. Use it for trivial snippets and reach for a proper minifier like Terser for anything real. It runs in your browser; nothing is uploaded.

What the JavaScript Minifier does

This tool makes a quick attempt at shrinking JavaScript by deleting comments and squeezing out the whitespace that the engine does not need: the indentation and the spaces around operators and punctuation. For simple, self-contained snippets the output is smaller code that still runs.

It is a fast, no-build way to compress a small script. But because it works by pattern-matching rather than understanding the language, it has real caveats. Treat it as a rough whitespace squeezer rather than a production minifier.

How it works

The tool runs a sequence of regular expressions: it removes block comments and line comments, strips the indentation at the start of each line, removes the whitespace on either side of operators and punctuation, and finally joins the lines together. There is a small guard so that the slashes in a URL like a https address are not mistaken for a comment.

It never parses the code. It does not know where a string starts or ends, what is inside a template literal, or where a regular expression sits. It reacts to characters only. That keeps it fast and dependency-free, and it is also why it can corrupt code that a real, parser-based minifier would handle safely.

Processing pipeline

  1. Remove comments. Delete block comments and line comments, with a guard so a URL is not treated as a comment.
  2. Collapse indentation. Strip the leading whitespace from each line.
  3. Trim around operators. Remove whitespace on both sides of operators and punctuation.
  4. Join the lines. Merge the lines into a compact result and trim it.

What it does (and where it fails)

• remove block and line comments (with a guard for a URL) • strip leading indentation • remove whitespace around operators and punctuation • join the lines into one it never parses, so it cannot see strings, templates or regex
Worked example
Before: function add(a, b) { return a + b; } After: function add(a,b){return a+b;}

That simple case works. But a line comment marker inside a string gets treated as a comment and truncates the string; a multi-line template literal has its indentation collapsed, changing the text; and removing spaces around the divide sign can corrupt a regular expression. A parser-based minifier avoids all of these.

Privacy

Your code never leaves your device. It is minified in your browser with no network request, so nothing is uploaded.

Because the work is local, you can run it on proprietary scripts without them being stored or sent anywhere, though real code deserves a proper minifier.

Nothing carries over between visits. Close the tab and the input and output are gone.

Standards & references

  • Minification: Removing characters the code does not need to run, such as whitespace and comments, to reduce file size. Done safely, it does not change behaviour; done naively, it can.
  • AST-based minifiers: Tools like Terser and esbuild parse the code into a syntax tree, leave strings, template literals and regex untouched, and can even rename variables. That safety is impossible with regular expressions alone.
  • Automatic semicolon insertion: JavaScript can infer some semicolons from line breaks. Joining lines can change behaviour where a semicolon was implied, which is another reason safe minification needs a parser.

Accuracy and limits

For a small, self-contained function the output is correct and smaller. On real code this naive regex approach can break things, so keep it for trivial snippets you can check by eye afterwards.

It does not understand strings. A line-comment marker inside a string is treated as the start of a comment, so the rest of that line, including the closing quote, is deleted and the code breaks. A parser knows the marker is inside a string and leaves it alone.

Template literals and regular expressions are at risk too. The indentation inside a multi-line template literal is collapsed, changing the text it produces, and removing the spaces around the divide sign can mangle a regular expression. The tool cannot tell these apart from ordinary code.

It does not rename variables or do any real optimisation, so even when it works it compresses less than a proper minifier. And by joining lines it can run into automatic-semicolon-insertion issues. For anything beyond a trivial snippet, use Terser or esbuild.

Where people use it

Squeezing a tiny snippet

Compress a few lines of simple JavaScript you can check by eye.

A quick size estimate

See roughly how much whitespace a small script carries.

Learning what minifying does

Watch comments and spacing disappear from simple code.

Inlining a trivial function

Shrink a small, string-free function for a quick paste.

When it fits, and when it doesn't

Good for

  • Compressing a trivial, string-free snippet
  • A rough idea of the whitespace saving
  • Learning what minification removes
  • Quick eyeball-and-check experiments

Not the best choice for

  • Any production or shared code
  • Scripts with strings, templates or regex
  • Code that relies on automatic semicolons
  • Maximum or safe compression

For anything real, use a parser-based minifier such as Terser or esbuild, which preserves strings, template literals and regex, handles semicolons safely, and can rename variables for far better compression. This tool is only for trivial snippets you will verify by hand.

Frequently asked questions

Can this break my JavaScript?
Yes. It is a naive regex minifier that does not parse the code, so it can corrupt strings that contain a comment marker, collapse multi-line template literals, and mangle regular expressions. Use it only for trivial snippets and check the result.
Why would it delete part of my string?
If a string contains the line-comment marker, the tool treats it as the start of a comment and removes the rest of the line, including the closing quote. It cannot tell the marker is inside a string, because it does not parse.
Does it work on a URL in a string?
There is a small guard so the slashes in an address like a https URL are not treated as a comment, so that common case survives. Other comment-like sequences inside strings are not protected.
What happens to template literals?
A multi-line template literal has its indentation stripped, which changes the text it produces. The tool does not know it is inside a template, so the whitespace there is removed like any other.
Does it rename variables like a real minifier?
No. It only removes whitespace and comments. It does not rename variables or do any structural optimisation, so it compresses far less than Terser or esbuild, which can mangle names safely.
Is it safe for production code?
No. For production, use a parser-based minifier such as Terser or esbuild. This tool is a rough whitespace squeezer suitable only for trivial snippets you can verify by hand.
Why did my regular expression stop working?
Removing the spaces around the divide sign can run a regular expression into the surrounding code or change how it is read. The tool cannot tell a regex from division, so it treats both the same.
What is automatic semicolon insertion and why does it matter?
JavaScript infers some semicolons from line breaks. By joining lines, the tool can remove a break where a semicolon was implied, changing behaviour. A parser-based minifier inserts the needed semicolons safely.
How much smaller will my script be?
For simple code, roughly the size of its whitespace and comments. It does not rename anything, so the saving is modest compared with a real minifier that also mangles names.
Is my code uploaded anywhere?
No. The JavaScript Minifier runs in this browser tab. Nothing is sent to a server. For real code, use a proper minifier anyway.
Can I reverse the minified output?
You can re-indent it with a formatter to make it readable, but comments are gone and any corruption from the minify is not recoverable. Keep your original source.
Then what is it good for?
Quickly squeezing a trivial, string-free snippet, or seeing what minification does. For anything you will ship, use Terser or esbuild instead.

References

Shrinks by stripping comments and removing whitespace around operators with regular expressions. This naive approach can corrupt strings, template literals and regex patterns, so it is for trivial snippets only.

PopularHot

JSON Formatter

Pretty-print messy or minified JSON with 2-space indents.

DeveloperOpen
Popular

JSON Validator

Paste JSON and find out whether it parses.

DeveloperOpen
Popular

Base64 Encoder

Standard Base64 for any text, emoji included.

DeveloperOpen
Popular

Hash Generator

MD5, SHA-1 and the SHA-2 hashes of any text, as you type.

DeveloperOpen
Popular

UUID Generator

Up to 500 random version 4 UUIDs at once.

DeveloperOpen
Trending

CSS Minifier

Strips comments and whitespace to put a stylesheet on one line.

DeveloperOpen

Back to the JavaScript Minifier

Paste, run and copy. The code stays in your browser and you don't need an account.