JavaScript Minifier
Light-minify JavaScript by stripping comments and whitespace.
Was this tool helpful?
Thanks for the feedback!
The JavaScript Minifier does a light clean-up of a script: it removes block comments and comment-only lines, strips indentation and deletes blank lines. Line breaks between statements are kept on purpose, which makes the result safe for code that relies on JavaScript's automatic semicolon insertion. It is meant for small scripts and inline snippets, not as a replacement for a build tool.
Before and after
This 176-character function:
/* Cart helpers */
function total(items) {
// sum line totals
let sum = 0;
for (const i of items) {
sum += i.price * i.qty; // add
}
return sum;
}
comes out as 108 characters, about 39% smaller:
function total(items) {
let sum = 0;
for (const i of items) {
sum += i.price * i.qty; // add
}
return sum;
}
Notice that // add survived. Only lines that consist entirely of a // comment are deleted; a comment after code on the same line stays, because removing it safely would require understanding strings and regular expressions.
What "light" means
- Variable and function names are not shortened.
- Spaces inside a line, such as around
=and+=, are kept. - Dead code,
console.logcalls and unused functions are not removed. - Each statement stays on its own line.
Full minifiers such as Terser, esbuild or UglifyJS parse the code, rename local variables to single letters and remove unreachable code, which usually gives much smaller files. If your project already has a bundler, use its minifier and keep this tool for quick jobs, such as tidying a script you paste into a CMS footer or a third-party tag manager.
Code that can break
This minifier works with text patterns, so a few constructs confuse it:
/*inside a string. Inconst a = "img/*"; foo(); /* note */ bar();the text from the string's/*to the comment's*/is treated as one comment, leavingconst a = "img bar();, which no longer runs.- Multi-line template literals. Indentation inside a backtick string is part of its value, and it is stripped. A line inside such a string that begins with
//is deleted entirely. - Regular expressions containing
/*hit the same problem as strings.
After minifying, run the page once and check the browser console for a syntax error. If one appears, look for these patterns first.
Where it fits
A light pass is useful for inline <script> blocks in landing pages and HTML emails, for sharing a snippet without its internal notes, or for a quick size check before setting up proper tooling. Because line breaks remain, it can't produce a one-line bookmarklet. Keep your commented original as the file you edit. For the rest of the page, the CSS Minifier and HTML Minifier apply the same idea to stylesheets and markup. Everything is processed in your browser.