Javascript Minify
JavaScript Minify removes comments and unnecessary whitespace from a script, shortens a few common expressions and returns a smaller file you can copy or download. Paste code or fetch a public URL; processing runs on the server, up to 200,000 characters per run.
Utilização Javascript Minify
Resultado
Ler o conteúdo
What this page is for
A readable script carries comments, indentation and spacing that the JavaScript engine does not need. This page sends the source to a server-side minifier and returns a compact version, with character counts for both sides. It is a source-to-source pass: JavaScript in, JavaScript out, with no execution and no bundling.
The page opens with JavaScript and Minify already selected. The language and mode selectors remain visible, so you can switch to another language or to Beautify, but everything below describes the JavaScript minifier.
What the minifier removes
Single-line comments and block comments are stripped. Extra spaces, tabs and indentation are collapsed. Unlike a full compressor, this minifier often keeps line breaks where they separate statements, so the output is not guaranteed to be one long line. In testing, a small script kept its statement breaks and only lost its indentation and comments.
Strings, template literals and regular expressions are extracted before the whitespace pass and restored afterwards. That protects comment-like text inside a URL, a regular expression or a string. A value such as "https://example.com" keeps its double slash instead of being mistaken for a comment.
Two rewrites you will notice
The minifier does more than delete whitespace. It also applies two small transformations:
- Property notation: a bracket access with a valid identifier, such as record["name"], can become record.name. The meaning is the same and the result is shorter.
- Boolean shortcuts: true becomes !0 and false becomes !1. These are valid JavaScript expressions that evaluate to the same values.
Neither rewrite renames your variables. A function called calculateTotal still appears as calculateTotal in the output. That makes the result easier to debug than a mangled bundle, but it also means the savings are smaller.
What it does not do
It does not rename or mangle identifiers, remove dead code, tree-shake modules, merge files, transpile newer syntax or generate source maps. It does not resolve imports or understand a bundler's module graph. It also does not validate your code: a script with a syntax error may pass through and fail only when the browser loads it.
If you need those features, use a build tool such as esbuild, Rollup or webpack. This page is for a quick shrink of a single file, not for producing a production bundle.
Strings, regular expressions and URLs
Comment stripping is the riskiest part of any regex-based minifier. A regular expression can contain two slashes, and a string can contain text that looks like a block comment opener. The library extracts these regions before it strips comments, so ordinary cases are handled. Complex or unusual patterns are still worth testing, especially regular expressions built from strings or split across lines.
If a script breaks after minification, compare the two versions around every regular expression and every string that contains punctuation. That is where a conservative minifier is most likely to differ from a parser-based one.
Modern JavaScript
Arrow functions, optional chaining, nullish coalescing, template literals and class fields passed through in testing. The minifier treats them as text and does not try to transform them. That is good news for modern code, but it is not a compatibility guarantee. Run the minified script in the browsers you support and check the console.
Automatic semicolon insertion is another area to watch. The minifier kept statement breaks in the cases tested, so a return followed by a value on the next line stayed intact. Do not rely on that for every construct: write explicit semicolons in code that will be minified, and test anything that depends on a newline.
Fetching a script from a URL
The URL tab asks the server to download a public script and place it in the editor. The fetch is capped at 200,000 characters. CDN links usually work; authenticated links, bot-protected links and signed URLs usually do not. The server fetches the raw response, so an HTML error page will arrive as HTML and produce a JavaScript syntax error rather than a useful result.
Step by step
- Paste the script into Manual Code, or load it through the URL tab.
- Confirm the language is JavaScript and the mode is Minify.
- Press Process Code.
- Compare the character badges and skim the output for the two rewrites described above.
- Copy the result, or download it as minify-output.js.
- Run the script in a test page and check the browser console before deploying it.
How much smaller will it be
A heavily commented, generously indented script can lose a meaningful share of its bytes. A file already processed by a bundler may lose very little, because the comments and whitespace are already gone. The exact number depends on comment density, identifier length and how much spacing the source used.
Do not treat the percentage as a quality score. A small file that still works is better than a large reduction that breaks an edge case.
Debugging minified JavaScript
Without a source map, a stack trace points into a compact file where many statements share a line. Keep the original source and deploy source maps from your build whenever possible. If you inherit a minified file with no source, beautify a copy to restore line breaks, then work from that copy. Beautifying cannot recover comments or the original names, and the JavaScript Beautify page has limits with modern syntax, so test the result.
Common errors
An empty output usually means the input was empty or the request exceeded the character cap. A syntax error after minification often comes from a regular expression that the extractor did not recognise, or from a template literal containing an unusual sequence. A script that runs but behaves differently often points to a newline-sensitive construct. Revert to the original and narrow down the section that changed.
Limits and privacy
One script per run, 200,000 characters maximum, JavaScript mode on this page. The code is sent to the server for processing and returned in the response; the tool does not store it. Large bundles belong in a local build step, where minification can run with source maps and the rest of the pipeline.
A worked example
To show what the minifier actually returns, this short function was passed through the same server-side library the page uses. The input was 355 characters, including a line comment, a block comment and four-space indentation:
// Calculate cart total
function cartTotal(items, discount) {
/* skip empty carts */
if (items.length === 0) {
return 0;
}
let total = 0;
for (const item of items) {
total += item["price"] * item.qty;
}
const hasDiscount = discount > 0 ? true : false;
return hasDiscount ? total * (1 - discount) : total;
}
The output was 210 characters, roughly 41 percent smaller:
function cartTotal(items,discount){if(items.length===0){return 0}
let total=0;for(const item of items){total+=item.price*item.qty}
const hasDiscount=discount>0?!0:!1;return hasDiscount?total*(1-discount):total}
What changed, line by line
- Both comments are gone, and the indentation and spaces around operators were collapsed.
item["price"]becameitem.price, andtrueandfalsebecame!0and!1.- The semicolon directly before a closing brace was dropped, as in
return 0}. JavaScript does not need a semicolon at the end of a block, so this is safe. - The output still has three lines. The library kept some statement breaks rather than joining everything into one line.
- Every name, including
cartTotal,itemsandhasDiscount, is unchanged.
Use a small sample like this to check the minifier against your own coding style before you process a whole file. If your style differs, for example you leave out semicolons, run a similar test with a short excerpt first.
Frequently Asked Questions
Perguntas frequentes
Comentários (0)
Descreva o problema que encontrou para que possamos investigar e melhorar a ferramenta.