JavaScript Minifier

Strip comments and indentation and join the lines it is safe to join, without renaming anything or risking automatic semicolon insertion.

Enable JavaScript to customise; default output below.

Nothing is uploaded. The minifying happens in this tab.

Mode

join lines merges lines where nothing can change meaning. safe keeps every line break.

Live preview script.min.js
/*! my-plugin v1.0 */
const panel=document.querySelector('.panel');function toggle(open){const next=open===undefined?!panel.hidden:open;panel.hidden=!next;panel.setAttribute('aria-expanded',String(next));return next}document.addEventListener('click',(event)=>{if(event.target.closest('.panel-toggle')){toggle()}});

/* 425 to 314 characters, 26.1% smaller. Comments and indentation only: nothing was renamed. */

Output is valid and updates as you type.

Strip the comments and indentation from JavaScript, and join the lines it is safe to join, without renaming anything.

How to use

  1. Paste the script. Nothing is uploaded; the work happens in this tab.
  2. Leave the mode on “join lines”. It merges a line onto the previous one only when the previous line cannot end a statement and the next cannot continue an expression.
  3. Switch to “safe” if you want every line break kept. The result is larger and impossible to get wrong.
  4. Read the footer. It says how much smaller the file got and reminds you what was not done.

Example

/*! my-plugin v1.0 */
// Toggle the panel.
const panel = document.querySelector( '.panel' );

becomes

/*! my-plugin v1.0 */
const panel=document.querySelector('.panel');

Comments and indentation are gone, the licence banner stays, and every identifier is untouched.

Pitfalls

  • This is not a build step. A real minifier renames local variables, drops dead code and inlines constants, and needs a parser to do it safely. esbuild or terser will beat this by a wide margin; use them where it matters.
  • Automatic semicolon insertion is why line joining is conservative. return on its own line means return;, and joining the next line onto it changes what a function returns. The joiner refuses that case, and refuses a line starting with (, [, `, +, - or /.
  • Minified code is unreadable in a stack trace. Ship a source map from a real build tool if you need to debug production.
  • A licence banner is a legal notice, not a comment. /*! is the convention every minifier respects, and this one keeps it.
  • Whitespace inside a string, a template literal or a regex is content. The tokeniser here keeps all three intact, which a regular expression based minifier cannot.
  • Minifying already minified code saves nothing and can only introduce risk. Check the reported size change.
  • The character count is not the transfer size. Gzip and Brotli do most of the work, and they compress repetitive whitespace very well, so the real saving from stripping it is smaller than it looks.
  • Nothing here transpiles. Modern syntax stays modern syntax.

Compatibility

The tokeniser handles strings, template literals including nested ones, regular expressions, comments, numeric separators, BigInt literals, private fields and every multi-character operator through the 2022 syntax. Output is the same JavaScript with less whitespace, so it runs wherever the input did. Everything is in your browser, with no upload.

Frequently asked questions

Why is my file only 30% smaller?
Because only comments and whitespace were removed. Renaming variables is where the rest of the saving lives, and that needs a parser.
Is it safe to run on any file?
Yes. Nothing is renamed or reordered, and line breaks are only removed where the language cannot treat them as a semicolon.
Should I minify by hand at all?
For a one-off snippet, or an inline script in a template, this is convenient. For an application, use a bundler.
Does it handle JSX or TypeScript?
No. Both need a parser that understands syntax JavaScript does not have.
What about use strict?
It is a string literal at the top of a file and is preserved exactly.
Weekly drops

New tools, when there are new tools

One email when something worth using ships. No schedule to fill, so no filler.

Your address goes nowhere else, and one click unsubscribes.