How to Minify JavaScript
Sometimes you just need a script on one line: inside a <script> tag, as a bookmarklet, or pasted into a CMS field that mangles line breaks. This page removes the comments and whitespace and leaves everything else alone. It produces the same result as the JavaScript Compressor, which is aimed at shrinking files before a deploy.
Paste or upload your JavaScript
Any plain JavaScript works, including ES modules and React JSX.
Example: a theme toggle before minifying
// Switch the page between light and dark theme
const toggle = document.querySelector("#theme-toggle");
toggle.addEventListener("click", () => {
// Flip the class on <html>
document.documentElement.classList.toggle("dark");
toggle.textContent = toggle.textContent === "Dark" ? "Light" : "Dark";
});Read the minified result
The result appears on its own a moment after you stop typing.
Example: the same code, minified
const toggle=document.querySelector("#theme-toggle");toggle.addEventListener("click",()=>{document.documentElement.classList.toggle("dark");toggle.textContent=toggle.textContent==="Dark"?"Light":"Dark";});305 bytes became 205. The two comments and the indentation account for most of the difference.
Copy or download it
Frequently Asked Questions
What's the difference between this and the JavaScript Compressor?
Nothing in the result. Both pages run the same steps: your code is parsed and printed back without comments, indentation or spaces it doesn't need. This page is set up for quick jobs like squeezing a snippet onto one line for an inline script tag, a bookmarklet or a config value, and it has a Clear button. The JavaScript Compressor is the same tool with the focus on file size before you deploy.
Does it rename my variables like Terser or esbuild?
No. Names stay exactly as you wrote them, and no dead code is removed. That keeps the output readable enough to debug, but a real build tool with name mangling will make a bundle smaller still. Use this page when you want a minified copy without setting up a build step.
Can minifying change what my code does?
It shouldn't, and it is built not to. The code is read with Babel's parser first, so things a naive whitespace stripper gets wrong are handled: return followed by a value on the next line stays two statements, a - -b keeps its space, and code written without semicolons gets the semicolons it needs. Strings, template literals and regexes are copied character for character, and numbers such as 8901240544102066246, 0x1F, 14.50 or a quoted "00501" keep their exact spelling.
Which comments are kept?
Comments that start with /*! or contain @license or @preserve stay, because licences usually require them. TypeScript hints like @ts-ignore and /// <reference> lines stay too. Every other comment is removed.
Why do I get an error instead of minified code?
The code has to parse before anything is removed, so a syntax error stops the run and you see the line and column with a caret under the problem. If you pasted JSON, CSS or TypeScript by mistake, the message says so. TypeScript needs compiling to JavaScript first, or you can use the TSX Minifier.
Is my code sent anywhere?
No. Minifying happens in your browser; the code isn't sent to our server and nothing is kept. Reload the page and the editor is empty again.
Related tools: the JavaScript Compressor, the TSX Minifier for TypeScript and the CSS Minifier.