JavaScript Minifier formula — how it actually works
Every answer on the JavaScript Minifier comes from three short rules and two things it never touches. There is no guessing and no hidden step: the tool reads your code once, splits it into quoted strings and everything else, and applies the rules to the everything-else parts only. The three numbers it shows — original, minified and saved — all follow from those rules.
Minified = comments removed, whitespace collapsed,
spaces around {} ( ) ; , : = removed
Strings copied untouched, spaces around + and - keptAt a glance
Rule 1: comments are removed
Both kinds of comment disappear. A // comment is removed from the slashes to the end of the line, and a /* */ comment is removed from the opening slashes to the closing ones, even if it spans several lines. In the default sample, // say hello and /* done */ are both gone, and nothing is left in their place. If a note matters to you, keep it in your original file, because the minified copy will not have it.
Rule 2: whitespace is collapsed
Every run of spaces, tabs and new lines becomes a single space. Five lines of code become one line, and a gap of four spaces becomes one space. Then rule 3 removes many of those single spaces as well. The collapse is why a file with generous line breaks shrinks so much: the line breaks themselves were bytes, and the tool takes them all out.
Rule 3: spaces around punctuation are removed
The tool removes the spaces around the punctuation {} ( ) ; , : and =. That is why function greet(name) { becomes function greet(name){ and why var menu = becomes var menu=. The spaces it removes are the ones that can never change what the code means, so the result still runs exactly as before. The spaces it keeps are the ones that could matter, which is the next section.
What is never touched
Two things are never changed. First, text inside quotes — double quotes, single quotes and backticks — is copied exactly as written, so the space inside “Hello, ” stays and the ${name} inside a template literal stays. Second, the spaces around + and - are kept, because removing them can change meaning: a - -b is not the same as a--b. A short test shows it: var a = 5; var b = 3; var c = a - -b; goes from 37 bytes to 29 bytes, and the spaces around the minus signs are still there in the result.
Worked example: the add function, step by step
Start with the add function, 59 bytes: a comment, four lines, and spaces everywhere. Rule 1 removes the comment // add two numbers, which takes the file down by 18 bytes. Rule 2 collapses the line breaks and the indentation into single spaces. Rule 3 removes the spaces around the braces, the comma and the parentheses. What is left is function add(a,b){return a + b;} which is 32 bytes. The saving is 27 bytes, or 46 percent, and the one space that survives inside the code is the pair around the +, kept by the never-touched rule.
The common mistake
People think a minifier renames variables and shortens names, because bigger minifiers do. This one does not: subtotal stays subtotal and tax stays tax, as the price script shows, where the minified output is 155 bytes and both names are still there in full. If you need shorter names, rename them yourself before you minify. Expecting the tool to do it is the fastest way to be disappointed by the byte count.
Frequently asked questions
What is the formula behind the JavaScript Minifier?
Comments removed, whitespace collapsed, and spaces around {} ( ) ; , : = removed. Strings are copied untouched and spaces around + and - are kept.
Which comments does it remove?
Both kinds: // line comments to the end of the line, and /* */ block comments, even across several lines.
Does it rename my variables?
No. Names like subtotal and tax stay exactly as written. The saving comes from comments, line breaks and spare spaces only.
Why are spaces around + and - kept?
Because removing them can change meaning: a - -b is not the same as a--b.
More guides on the JavaScript Minifier
- How to use the JavaScript Minifier — step by step
- JavaScript Minifier explained with worked examples
- JavaScript Minifier for beginners — start here
- Common mistakes people make with the JavaScript Minifier
- JavaScript Minifier vs doing it by hand — which is faster?
- JavaScript Minifier — the complete guide
- How to get an accurate result from the JavaScript Minifier
- JavaScript Minifier for students — with examples
- JavaScript Minifier for homeowners and DIYers
Try the JavaScript Minifier — free, in your browser.