About the JavaScript Minifier
Comments and whitespace make scripts readable for people but add weight for every visitor who downloads them. This JavaScript minifier removes them safely, detecting strings, template literals and regular expressions so that characters inside them are never altered, and it never renames variables, which keeps the result predictable and easy to debug. It is ideal for small scripts, snippets and bookmarklets. For large applications, build tools that also rename variables will shrink files further. To make minified code readable again, use the JavaScript Formatter, and for stylesheets, the CSS Minifier.
One thing this minifier deliberately leaves alone is the names of your variables and functions. Professional build tools make files far smaller by shortening descriptive names to single letters, but doing that safely means parsing the entire program and understanding every scope in it. A renaming step that is right most of the time produces errors that only appear in production, which is the worst place to discover them.
The reduction is therefore moderate. Well-commented source usually loses between a fifth and two fifths of its size, and the gain after gzip is smaller still. A production application deserves a full minifier in its build pipeline, but when all you need is a lighter version of a short script that will be pasted into a page, this tool gets there without any chance of breaking it. Keep the readable original next to it for future edits. Keep the original, readable file in your project and publish only the minified copy, because minified code is very hard to edit by hand.
How to use the JavaScript Minifier
- Paste the scriptReadable source code, complete with its comments, blank lines and indentation.
- Decide what to stripComments, blank lines and indentation can each be removed, and licence headers can be kept.
- Run MinifyThe compact code is shown with a size comparison.
- Take the outputCopy it or download the minified file.
JavaScript Minifier features
- Line comments and block comments removed
- Whitespace and indentation collapsed
- Strings, template literals and regular expressions left exactly as written
- Semicolons kept, so nothing depends on automatic insertion
- Option to keep licence comments
- Variable names never changed, so the code cannot break
- Before size, after size and percentage saved
Common uses
- Slimming down a short script before embedding it in a page
- Removing your comments from code you are about to share
- Reducing the weight of a chat widget or tracking tag you embed
- Measuring how much of a file is made up of comments
- Compressing a script where there is no build step
Minifying JavaScript in practice: inline head scripts, bookmarklets, loading without blocking, third-party weight and secrets in front-end code
Safe whitespace and comment removal is ideal for small scripts that live inside pages: a theme switcher in the head, a bookmarklet, a tracking snippet. Large applications need a full build tool. This guide minifies an inline script that prevents a dark-mode flash, then covers bookmarklets, loading scripts without blocking pages, the weight of third-party scripts, source maps and why minified code never hides secrets.
A worked example: an inline theme script
A site with a dark mode places a short script in the head that applies the visitor's saved theme before the page is drawn, so dark-mode users never see a white flash. With explanatory comments, the script is 666 bytes. Removing comments and whitespace brings it to 314 bytes, more than half smaller, with exactly the same behaviour. Because inline scripts are delivered with every page, small savings like this repeat on every visit.
Bookmarklets
A bookmarklet is JavaScript saved as a bookmark's address, starting with javascript:. It must be a single line, so minifying is the natural first step, followed by URL-encoding characters such as spaces and percent signs. Keep the readable version in a file for future changes, since editing a minified one-line bookmarklet is painful.
Loading scripts without blocking
How a script loads often matters more than its size. A script tag in the head without attributes stops the browser building the page until the script downloads and runs. Adding defer lets the page continue and runs the script once the document is parsed, in order, which suits most site scripts. The async attribute suits independent scripts such as analytics.
The weight of third-party scripts
On many sites, analytics, advertising, chat and social widgets add far more JavaScript than the site's own code. Minifying your own scripts while loading several hundred kilobytes of third-party code achieves little. Reviewing which external scripts are truly needed, and loading the rest only when used, often brings the biggest improvement.
Production builds
For applications with many modules, bundlers and full minifiers do much more than this tool: they rename variables safely, remove code that is never used and combine modules efficiently. Use them for production builds of larger projects, and keep this tool for snippets and small scripts where a build pipeline would be overkill.
Source maps
Source maps link minified files back to the original source, so error reports and debugging show real file names and lines. Generate them in builds, and decide whether to publish them openly or keep them for internal debugging. Without source maps, an error in minified code points to line 1 and a column number that helps nobody.
Minified code is not secret
Minification makes code shorter, not private. Anyone can format it and read it again in seconds. API keys, passwords and secret tokens must never be placed in front-end JavaScript, minified or not. Keys that must appear in browsers, such as those for maps, should be restricted to your domain and limited to read-only operations.
Testing the result
After minifying, run the script in the page and check the browser console for errors. Pay attention to places where the original code relied on line breaks rather than semicolons, and to comments that contained licence text you are required to keep. A quick functional test is the final step for any script you are about to publish.
Common mistakes
- Minifying scripts while leaving them render-blocking.
- Expecting minification to hide keys or logic.
- Editing minified files instead of the readable source.
- Ignoring third-party scripts that outweigh your own code.
Related tools
To read minified code, use JavaScript Formatter. To encode bookmarklets, try URL Encoder, and to minify the page itself, HTML Minifier.
Inline event handlers
Moving onclick and similar attributes out of HTML into a script file keeps behaviour in one place, allows the code to be minified and cached, and lets the site use a strict content security policy that blocks inline scripts. It also makes the HTML shorter and easier to read.
Module scripts
Scripts loaded with type="module" are deferred automatically, so they never block the page from being built. Modern browsers all support them, and they allow code to be split into files that import each other. Older browsers ignore module scripts, which is rarely a concern for current audiences.
Removing debug logging
Console logging left in production code clutters the console and can expose internal details. Full minifiers can strip console calls automatically through an option, and linting rules can warn about them before code is committed. For small snippets, remove them by hand before minifying.
The real cost of scripts
JavaScript costs more than its download size suggests, because the browser must parse, compile and run it, which takes noticeable time on budget phones. A hundred kilobytes of script typically delays a page more than a hundred kilobytes of image. Shipping less script is the most effective optimisation of all.
Privacy
Minification runs in your browser. Your code is not uploaded or stored.
Frequently asked questions
Why are variable names not shortened?
Renaming safely means parsing the whole program and resolving every scope, including closures and global names other scripts depend on. Getting that slightly wrong creates errors that only appear when the code runs, so this tool simply never renames anything.
What kind of size reduction can I expect?
On source with plenty of comments, often a fifth to two fifths of the file disappears, and the effect after gzip is smaller. Build tools that also shorten names and delete unused code usually manage two or three times as much.
Is the result safe to put on a website?
For short scripts and snippets, yes: the tool only deletes comments and spacing that the JavaScript engine ignores anyway, so the meaning cannot change. Large applications with many files are still better served by a dedicated minifier in a build.
Can licence comments be kept?
Yes, with the licence option switched on, block comments that start with an exclamation mark are preserved. That is the usual convention for copyright and licence headers you are required to keep.
Does it handle modern JavaScript?
Yes. Backtick strings with embedded expressions, class syntax, async functions, the ?. operator and other recent additions are all fine, because the tool reads the code piece by piece and never needs a full grammar of the language.
Does the script I paste get uploaded?
No. Minification happens entirely within your browser, so the script you paste stays on your device and is never stored, logged or transmitted to any server.