JavaScript Minifier — Compress JS Online
Compress JavaScript to Reduce File Size
Minifying JavaScript removes unnecessary characters—spaces, line breaks, comments—without changing functionality, shrinking file size by 30–70% in most cases. Our free JavaScript Minifier compresses your code in your browser with full control over ECMA targets and compression strategies. Paste your script, choose your options, click minify, and copy the compact result — all without sending anything to a server.
Why Minify JavaScript?
Every kilobyte matters on the web. Smaller scripts load faster, parse quicker, and reduce bandwidth costs. Modern build tools (webpack, Vite, Rollup) minify automatically, but sometimes you need to minify a standalone script, patch a legacy file, or check what a minified snippet does.
Hand-minifying is error-prone and tedious. You have to track variable names, remove comments, collapse whitespace, and manage quoted strings carefully. A careless deletion breaks the code. This tool uses Terser, the same minifier that powers production JavaScript bundlers, so the output is always correct and optimized.
How to Use the Tool
The widget presents a source textarea with line numbers, a set of minify options, and an output textarea.
Step 1 — Paste your JavaScript. Click the JavaScript source box and paste your code. Multi-line functions, variable declarations, comments, and formatting all work. The line numbers on the left help you navigate the code.
Step 2 — Choose your minify options. Several settings control how aggressively Terser compresses:
- ECMA version — Select
5,2015, or2016. Newer versions enable modern syntax optimization but may break on older browsers. Default is2015. - Treat as module — Enable if your code is an ES6 module (has
import/export). Helps Terser understand scope. - Mangle top-level names — Renames global variables and functions to shorter names. Smaller output but breaks external API calls, so disable if the script is a library.
- Keep class names — Preserves class names even when mangling. Useful if code introspects class names.
- Keep function names — Preserves function names. Useful for debugging or if code references function names.
- IE8 compatibility — Uses older JavaScript syntax for Internet Explorer 8 support. Rarely needed unless supporting legacy browsers.
- Safari 10 workaround — Enables quirk fixes for Safari 10. Rarely needed on modern browsers.
All checkboxes default to off for maximum compression.
Step 3 — Minify your code. Click Minify JavaScript. Terser processes the code and displays the result in the Minified JavaScript output box, along with a message showing the size reduction.
Step 4 — Copy and use. Once minified, click Copy minified JavaScript to send the output to your clipboard. If you want to start over, hit Clear input to reset everything.
Everything runs in your browser. Your JavaScript never leaves the page and is never sent to a server.
Worked Example
Input:
function hello(name) {
// Greeting function
console.log("Hello, " + name);
return "Hello, " + name;
}
hello("World");
Output (default options):
function hello(n){console.log("Hello, "+n);return"Hello, "+n}hello("World");
Output (Keep function names ON, Mangle top-level OFF):
function hello(n){console.log("Hello, "+n);return"Hello, "+n}hello("World");
Notice how the parameter name becomes n (a shorter variable), but hello and console.log are recognized as built-in or API calls and are left alone.
What Each Option Does
- ECMA version — Controls the target JavaScript standard. ES5 is safest but produces larger output. ES2015+ unlocks modern syntax compression.
- Treat as module — If enabled, top-level declarations are scoped as module exports, not globals. Essential for ES6 modules.
- Mangle top-level names — Renames global functions and variables (
myFunction→a). Breaks code that calls your function by name from outside (e.g.,<button onclick="myFunction()">breaks). Use only for self-contained scripts. - Keep class names — Prevents class name mangling. Some frameworks inspect class names at runtime, so this keeps them readable.
- Keep function names — Prevents function name mangling. Useful if you have a function that introspects its own
nameproperty or is called via string lookup. - IE8 compatibility — Older syntax for ancient browsers. Modern projects can ignore this.
- Safari 10 workaround — Fixes obscure Safari 10 parsing quirks. Rarely needed; use only if you see Safari 10 bugs.
FAQ
Does this tool upload my JavaScript anywhere? No. All minification happens locally in your browser using Terser compiled to WebAssembly. Your code never touches a server.
What if my code has errors? The tool displays an error message and leaves the output empty. Check your syntax—missing semicolons, unclosed brackets, and malformed strings are common culprits.
Will minified code still work the same way? Yes, minified code is functionally identical to the original. Minification only removes comments and shortens variable names; it does not change logic.
Does minification break debugging? Yes. Minified code is hard to read in a debugger because variable names are shortened and formatting is removed. For development, keep original source maps or use unminified code. Use minified code only in production.
What's the compression ratio? Most JavaScript minifies to 40–60% of original size. Library code and function-heavy scripts compress more. Heavily commented code compresses less. The tool shows the before/after byte count.
Can I minify HTML or CSS too? Yes. Use the HTML Minifier and CSS Minifier tools for those formats.
Should I always mangle top-level names?
Only if your code is self-contained and not called from outside (e.g., from HTML onclick attributes or global namespaces). If your code exports functions for others to use, disable this option.
Related Tools
- HTML Minifier — Compress HTML markup
- CSS Minifier — Compress stylesheets
- HTML Formatter — Format and beautify HTML
- CSS Formatter — Format and beautify CSS