EthicalFusion

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.log calls 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. In const a = "img/*"; foo(); /* note */ bar(); the text from the string's /* to the comment's */ is treated as one comment, leaving const 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.

Frequently asked questions

Why does the minified JavaScript still have line breaks?
Line breaks are kept deliberately so that code without semicolons keeps working. Only indentation, blank lines and comments are removed.
Does the JavaScript minifier rename variables?
No. Names stay exactly as written. Use a full minifier such as Terser or esbuild if you want variables shortened.
Why was my // comment not removed?
Only lines made up entirely of a // comment are deleted. A comment placed after code on the same line is left in place.
Can minifying break my script?
Yes, in rare cases: a /* inside a string or regular expression, or a multi-line template literal, can be damaged. Test the result in the browser before publishing.
Is this minifier good enough for production?
For small scripts it trims comments and whitespace well. For larger apps, a bundler with a proper minifier produces smaller and safer output.

Related tools

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.