What is a JS minifier?
A JS minifier takes JavaScript code and makes it smaller without changing what it does. It removes the parts humans need but machines do not: comments, blank lines, and extra spaces. The code that comes out runs exactly the same as the code that went in — it is just shorter, which means it downloads faster and takes up less space.
This tool does light minification. It strips single-line and block comments, then collapses unnecessary whitespace: extra spaces, tabs, and line breaks become single spaces or disappear entirely where the grammar allows it. It does not rename variables, shorten property names, or restructure your code the way a full parser-based minifier does — that work needs a real parser, and this page says so plainly instead of pretending otherwise.
The default example shows the idea in one line of code. The function greet takes a name and returns a greeting, with a short comment above the return. The original is 72 characters long. After light minification it is 48 characters — 24 characters saved, about a third of the file — and the greeting it returns is identical, spaces inside the quoted text included.
Minifiers are used everywhere on the modern web. When you load a website, the JavaScript it sends you is almost always minified, because a smaller file means a faster page, less data used, and a better experience on slow connections. Build tools do this automatically, but when you have a snippet to shrink by hand, a minifier like this one does the job in seconds.
How this JS minifier works
The minifier works in two passes, and both are careful about strings. The first pass walks through the code character by character, tracking whether it is inside a quoted string — single quotes, double quotes, or backticks. Outside strings, a double slash starts a comment that runs to the end of the line, and a slash-star pair starts a block comment that runs to the closing star-slash. Both are removed. Inside strings, the same characters are ordinary text and are left alone.
That string-awareness matters more than it sounds. A web address like 'http://example.com' contains two slashes, and a naive minifier that strips everything after double slashes would destroy it. This one does not: paste var s = 'http://x'; // comment and the comment disappears while the address inside the quotes survives untouched. The same care applies to text like 'Hello, ' — the space after the comma is inside a string, so it stays.
The second pass collapses whitespace. Runs of spaces, tabs, and newlines become a single space, but only where a space is actually needed — between two words, for example, or between a keyword and a string. Where the grammar does not need a space, it is dropped: var x = 1; becomes var x=1;, and function greet(name) { becomes function greet(name){. The result is valid JavaScript that a browser runs exactly as before.
The level selector changes what the second pass does. Light mode runs both passes. Comments-only mode runs only the first pass, so the layout of your code stays exactly as you wrote it and only the comments go. That mode is handy when you want to clean up a file you will keep reading, rather than shrink it for delivery.
The size numbers, and what they mean
The three numbers at the top tell the whole story. Original size is the character count of the code you pasted. Minified size is the count after minification. Size saved is the percentage difference between the two — the share of the file that was comments and whitespace rather than code. On the default example, 72 characters become 48, so the saving is 33.3%.
How much you save depends on how the code was written. Heavily commented code with generous spacing can shrink by half or more. Try the second example: var x = 1; // comment on one line and var y = 2; on the next is 32 characters, and it minifies to var x=1;var y=2; — 16 characters, a 50% saving. Dense code with few comments might save only 5 or 10%, because there is simply less dead weight to remove.
Do not confuse character count with file size on the wire. A minified file is usually served with gzip compression on top, and gzip squeezes out much of the repetition that minification leaves behind, so the real download saving is smaller than the character saving suggests. Even so, minification still helps: a smaller file compresses better, and every byte saved is a byte a phone on a slow connection does not have to wait for.
One number worth watching is the ratio, not the raw saving. If a 2,000-character file saves 40%, that is 800 characters gone — meaningful. If a 50-character snippet saves 40%, that is 20 characters — correct, but hardly worth a build step. Use the percentage to judge whether minifying a particular file is worth the effort, and the character counts to check the result is what you expected.
A worked example: shrinking a greeting function
Start with the default code on this page. It is a small function: function greet(name) { on the first line, a comment saying Say hello, then return 'Hello, ' + name + '!';, then the closing brace. Count it up and it is 72 characters, including the newlines and the spaces used for indentation.
Run light minification and the comment disappears first. Then the whitespace pass removes the newline and indent before return, the spaces around the plus sign and the equals, and the spaces around the braces. What is left is function greet(name){return 'Hello, '+name+'!';} — 48 characters. Paste it into a browser console, call greet('Ada'), and it returns Hello, Ada! exactly as the original did.
Now try the same code with comments-only mode. The comment text goes, but the indentation and line breaks stay, so the result is the original layout with one blank line left behind — 60 characters instead of 72. That is the mode to use when the code is still being read by people and you only want the comments gone.
Try a third input to see the string protection at work: var s = 'http://x'; // c. The comment after the string is removed, the address inside the string is kept whole, and the spaces around the equals sign go. The result is var s='http://x'; — the address still works, because the minifier never touches text inside quotes.
Who uses a JS minifier, and why
Web developers use minifiers every day, usually without thinking about it. The build step of a website — the part that turns source files into the files a server actually sends — minifies JavaScript as a matter of routine. Smaller files mean faster pages, lower bandwidth bills, and less waiting for visitors on mobile connections. A site that loads in two seconds instead of four keeps noticeably more of its visitors.
Bloggers and site owners who paste small scripts into their pages use a minifier by hand. A tracking snippet, a small widget, or a custom bit of interactivity is often pasted into a page template, and minifying it keeps the page HTML lean. The same applies to bookmarklets — small programs stored in a browser bookmark — where every character counts because the whole program lives in a single line.
Teachers and learners use minifiers to see what JavaScript looks like without its scaffolding. Stripping the comments and spacing from a function shows the bare logic underneath, which can make a confusing piece of code easier to read — or, just as often, shows why the comments were there in the first place. Running code through a minifier is a quick way to check whether you actually understand what each part does.
It also helps when sharing code in places with size limits. A chat message, a forum post, or a code snippet in a comment thread all have practical limits, and a minified version fits where the original would not. Just remember to keep the original somewhere safe — minified code is much harder to edit later, which is why nobody writes it by hand.
JS minifier vs a full parser minifier
The honest difference is what each one is allowed to change. This tool removes comments and whitespace, which are the only parts of a program that can be deleted without understanding the code. A full parser minifier goes further: it parses the code into a syntax tree, renames local variables to single letters, drops unused code, and folds constant expressions. That can shrink a file by 60 or 70%, where light minification might manage 20 to 40%.
The trade is safety against size. Because this tool never renames anything, it cannot break code by renaming a variable that another file depends on, and it cannot misread a tricky construct. A parser minifier is safe too when it is configured right, but it needs the code to parse cleanly — a syntax error anywhere stops it, while light minification will happily process code with a typo in it and hand it back still broken in the same way.
For a quick job, light minification is usually enough. If you are shrinking a snippet for a page, a bookmarklet, or a message, the comment-and-whitespace saving is the bulk of what you would get anyway. For a production build with thousands of lines, use a real build tool — it will do the renaming and dead-code removal as well. The two approaches are not rivals; they are different tools for different sizes of job.
If you need the opposite direction — taking compressed code and making it readable again — the companion tools on this site do that. A JSON formatter handles data in the same spirit, and a text formatter tidies plain text, while the JS formatter in this same Developer Tools group re-indents JavaScript so it can be read again. If you want to measure the result, a word counter counts the words in a snippet and a character counter counts its characters — both handy when a size limit is measured in characters rather than words.
Common minifier mistakes (and how to dodge them)
The first mistake is minifying code you still need to edit. Minified code is hard to read and harder to change, and editing it directly is how bugs are born. Always keep the original source file, minify a copy, and treat the minified version as a disposable output. If you lose the original, a formatter can restore the layout, but it cannot restore the comments or the original variable names.
Second: assuming minification hides your code. It does not. Minified code is still perfectly readable to anyone who pastes it into a formatter, and variable names that matter — like API keys or passwords — are still there in plain text. Never put secrets in client-side JavaScript, minified or not. If a string in your code would embarrass you if a stranger read it, it does not belong in the file.
Third: minifying code with a syntax error and expecting the error to go away. Minification preserves behaviour, including broken behaviour. If the original throws an error, the minified version throws the same error, only now the error message points at a single long line that is much harder to debug. Test the original first, then minify.
Fourth: forgetting that comments sometimes carry meaning. Licence headers, copyright notices, and the odd comment that says do not remove this are comments too, and a minifier removes them like any other. If a licence requires its notice to stay in the file, keep a copy of it, or use a minifier that preserves legal comments. This tool removes all comments, so check the output before you ship it.
How to use this JS minifier
Paste your JavaScript into the box at the top of this page. The default example is already there, so you can see the result before you type anything: 72 characters in, 48 out, a 33.3% saving. Replace it with your own code and the numbers update instantly as you type — there is no button to press.
Pick the level that fits the job. Light mode is the default and the one most people want: comments gone, whitespace collapsed. Comments-only mode keeps your layout and removes only the comments, which is better when the code is still being read by humans. Switch between them and watch the minified size change.
Copy the result from the output box underneath. It is a single block of text you can paste into a page, a bookmarklet, or a message. If the output looks wrong — a string that lost a space, a line that vanished — check the input for an unclosed quote, because an unclosed string makes the minifier treat the rest of the file as string content.
Everything runs in your browser. Your code never leaves your device, is never uploaded, and is never stored. You can minify proprietary code, half-finished code, or code with secrets in it without worrying about where it goes — it goes nowhere. Close the page and it is gone.