JavaScript Minifier
A light, safe minify for small scripts: comments and indentation go, strings and regular expressions stay as they are.
Paste code and press Minify.
How to minify JavaScript
- Paste your script into the input box, or press Load a sample to try a small example first.
- Leave Keep /*! licence */ comments ticked if the code includes a copyright notice that must stay with it. All other comments are removed.
- Press Minify JavaScript. The line underneath shows the size before and after and the percentage saved.
- Press Copy result, paste the output into your page or file, and test it before you publish.
Worked example
The sample is a basket helper of 415 bytes. It starts with a licence comment, includes a full-line comment and a trailing comment, and is indented for reading. It defines a function that adds up the price times quantity of each item and works out VAT. After minifying, the result line reads 415 bytes → 285 bytes (31.3% smaller). The licence line is still at the top, the other comments have gone and the indentation has been removed.
To prove the code still does the same job, we ran both versions on the sample basket of two items at £12.50 and three at £4 with a VAT rate of 20. Each returned a net total of 37, VAT of 7.4 and a gross total of 44.4. Matching results like this are the check you should always make with your own scripts.
How it works
This is a deliberately light, safe minifier. It reads the code piece by piece and recognises strings, template literals and regular expressions, so a // inside a web address or a pattern is never mistaken for a comment. Comments and indentation are removed, and spaces are kept only where two words would otherwise merge, as in var total or return net. Line breaks are kept where JavaScript's automatic semicolon rules might need them, and removed where they cannot matter, such as after an opening brace or a comma. Variable names are never shortened, so error messages and debugging remain readable.
Light minifying compared with a full build tool
Tools such as Terser or esbuild go further: they rename variables, remove unused code and rewrite expressions, often saving more than half the size. They also need installing and configuring. This page suits the many cases where that is overkill, such as an analytics snippet, a small widget, a script pasted into a theme option or a bookmarklet.
Tips
- Always keep the readable original. Minified code is hard to edit and debug.
- Test the minified output in the browser console or on a staging page before going live.
- If your code relies on missing semicolons in unusual places, add them first. Clean code minifies most safely.
- Serve scripts with gzip or Brotli compression for a further large saving.
- Load non-essential scripts with the
deferattribute, so they do not hold up the first paint of the page.
Frequently asked questions
Will it break my code?
It is designed to be cautious, and it keeps line breaks wherever they could matter. Unusual syntax can still trip any minifier, so test the result.
Does it support modern JavaScript?
Yes. Arrow functions, template literals, classes and modules are passed through, because the tool only removes whitespace and comments.
Why are some line breaks still there?
JavaScript can insert semicolons automatically at the end of a line. Where removing a line break might change how the code is read, the minifier keeps it. One line break costs a single byte, so safety wins.
Can it make my code unreadable to protect it?
No. Minifying is not obfuscation, and anyone can reformat the result. Never place passwords or private API keys in code that runs in a browser.
Is my code uploaded?
No. Everything runs in your browser tab, and nothing is stored after you close it.