About JavaScript Minifier
This is a light, text-based minifier. It deletes /* block */ and // line comments, trims every line, drops blank lines, and removes the spaces and line breaks around braces, semicolons and commas. In the worked examples below that removes 28% to 40% of the characters. It does not parse the code, so it never renames variables, removes unused functions or shortens expressions, and the output stays readable. The same simplicity means it applies its rules inside string literals too: a string containing a comma followed by a space, or a brace or // with spaces around it, will be changed. Read the limits below and test the result before you ship it.
JavaScript minify examples with real output
Each result is the tool's exact output for the input described, with the figures from the Savings panel.
A function with a line comment
function add(a, b) { // sum two numbers / return a + b; } then const result = add(2, 3); and console.log(result); written over 7 lines
function add(a,b){return a + b;}const result = add(2,3);console.log(result); Before 107 chars, After 76 chars, Saved 29%.
A loop with a block comment and trailing comment
var total = 0; /* running sum */ then for (var i = 1; i <= 10; i++) { containing total += i; // add indented 4 spaces, a closing brace, then console.log(total); written over 5 lines
var total = 0;for (var i = 1;i <= 10;i++){total += i;}console.log(total); Before 109 chars, After 73 chars, Saved 33%.
An exported arrow function with a JSDoc block
A 4-line /** Format a price. @param {number} n */ block above export const price = (n) => { ... } with two return statements
export const price = (n) =>{if (n === 0) return "free";return "$" + n.toFixed(2);}; Before 139 chars, After 83 chars, Saved 40%.
Code written without semicolons
const x = 10 // ten, const y = 20 // twenty, console.log(x + y) on three lines
The three statements stay on three separate lines with the comments gone. Before 61 chars, After 44 chars, Saved 28%.
Line breaks are kept unless they sit next to a brace, semicolon or comma, so statements that rely on a line break to end them are not glued together.
What gets removed and what stays
| In your code | What happens | Example |
|---|---|---|
| /* block comments */ | Removed, including multi-line and JSDoc blocks | /** docs */ disappears |
| /*! licence banners */ | Removed like any other block comment | /*! license MIT */ var a = 1; becomes var a = 1; |
| // line comments | Removed to the end of the line | x = 1; // note becomes x = 1; |
| // after a colon or quote | Kept | "https://example.com" and "//cdn.example.com/app.js" are unchanged |
| Indentation and trailing spaces | Removed from every line | 4-space indents vanish |
| Blank lines | Removed | Gaps between functions close up |
| Space or line break around { } ; , | Removed | add(a, b) { becomes add(a,b){ |
| Spaces around operators | Kept | a + b and x === 3 are unchanged |
| Line breaks between statements with no ; or } | Kept | const a = 1 stays on its own line |
| Variable and function names | Kept | result stays result |
Three inputs this minifier gets wrong
These are real outputs. If your code contains one of these patterns, fix it by hand afterwards or use a parser-based tool.
Punctuation with spaces inside a string
const msg = "Hello, world; bye";
const msg = "Hello,world;bye";
The spaces after the comma and semicolon are removed even though they are inside quotes, so the text shown to users changes. The sample greet function is affected the same way: its template string loses the space after the comma, so the greeting reads Hello,Ada! instead of Hello, Ada!
Two slashes after a space inside a string
const note = "a // b";
const note = "a
Everything from // to the end of the line is treated as a comment, leaving an unterminated string and a syntax error. The same happens after any character other than a colon, quote, backtick or backslash, so "a//b" is cut as well.
An object literal with no semicolon after it
const o = { a: 1 } on one line and const p = 2 on the next
const o ={a: 1}const p = 2
The line break after the closing brace is removed, which joins two statements into invalid code. Ending such lines with a semicolon avoids it.
Whitespace minifier vs Terser, esbuild and SWC
Parser-based minifiers read the code into a syntax tree before rewriting it, which is what lets them make deeper changes safely.
| Capability | This tool | Parser-based minifier |
|---|---|---|
| Remove comments and indentation | Yes | Yes |
| Leave string and regex contents untouched | Not always | Yes |
| Remove spaces around operators | No | Yes |
| Shorten local variable names (mangling) | No | Yes |
| Remove unreachable and unused code | No | Yes |
| Keep licence comments on request | No | Yes |
| Produce a source map | No | Yes |
| Needs installing or a build step | No | Usually |
When a quick comment and whitespace pass is the right tool
- Stripping developer comments, ticket numbers and TODO notes from a small script before it goes on a public page.
- Fitting a snippet into a field with a character limit, such as a tag manager, a CMS script box or a bookmarklet.
- Shrinking an inline script on a static page that has no build pipeline.
- Seeing how much of a file is comments and layout: the Saved percentage is a close guide to that share.
- For application bundles, use the minifier built into your bundler instead. It will save more and will not touch your strings.
How to get your result
- 1
Paste your code into the JavaScript box, replacing the sample greet function.
- 2
Read the Minified JavaScript panel. There are no options to set.
- 3
Check the Savings panel for the Before and After character counts and the percentage Saved.
- 4
Compare any string literals in the output with the original, then run the minified code once to confirm it behaves the same.
- 5
Copy the result, or download it as script.min.js.