JavaScript Minifier
Minify JS for production with Terser. This JavaScript minifier compresses and mangles your code, keeps license comments, can create a source map and reports syntax errors with the exact line and column.
Press Ctrl+Enter (⌘+Enter on Mac) to run now.
Minify JavaScript Online
JavaScript is usually the most expensive part of a page: it has to be downloaded, parsed and compiled before it runs. This JavaScript minifier makes that cost smaller by removing whitespace and comments, rewriting code into shorter equivalent forms and renaming local variables to one or two letters.
It runs Terser, the minifier that webpack uses by default in production mode, directly in your browser. You can keep license comments, choose the ECMAScript version the output may use, keep function and class names that your code relies on, and download a source map so the minified code can still be debugged. Syntax errors are shown with the exact line and column.
How to minify JavaScript
- Paste your JavaScript or open a
.jsor.mjsfile. - Keep Compress and Mangle on for the smallest output. Tick “ES module” for code that uses
importorexport. - Set the ECMAScript version your users’ browsers support and choose which comments to keep.
- Check the savings panel for the original size, the minified size, the percentage saved and the gzipped size.
- Copy or download
script.min.js, plusscript.min.js.mapif you enabled the source map.
What Terser does to your code
- Removes whitespace and comments. License comments (those starting with
/*!or containing@license,@preserveor@copyright) are kept by default. - Compresses: evaluates constant expressions, drops unreachable code such as
if (false) { … }, inlines single-use values, joins consecutive statements with commas and removesdebuggerstatements. - Mangles: renames local variables and parameters, for example
function calculateTotal(items, discount)becomesfunction calculateTotal(t,c). Property names are never renamed, so object keys and APIs keep working. - Leaves globals alone by default: top-level names may be used by other scripts on the page, so they are only renamed when you tick “Mangle top-level names” or “ES module”.
JavaScript minifier options
| Option | What it does | When to change it |
|---|---|---|
| ECMAScript version | Lets Terser use newer, shorter syntax up to that version (for example arrow functions from ES2015). It never converts newer syntax down to older versions. | Pick ES5 only if the output must avoid any syntax that the input did not already use |
| Comments | Keep license comments, remove all, or keep all | Keep license comments to respect open-source licenses |
| Compress passes | Runs the compressor up to three times; later passes can find extra savings | Try 2 or 3 for large files |
| Mangle top-level names | Also renames top-level functions and variables | Only for self-contained scripts that nothing else calls |
| ES module | Parses as a module: strict mode and top-level names treated as private | Code with import / export |
| Keep function / class names | Preserves fn.name and Class.name | Frameworks, loggers or error reports that read names |
| Drop console calls | Removes console.* calls | Production builds without debug logging |
| Source map | Creates a map from minified positions to your original code | Whenever you need readable stack traces |
Syntax errors and source maps
Terser parses the whole file before it minifies anything, so it doubles as a quick syntax checker. An error such as Unexpected token: punc (;) is shown with its line and column and a caret under the exact character. Common causes are a missing bracket, a stray comma or TypeScript type annotations, which must be compiled to JavaScript first. When “Source map” is ticked, the minified file ends with a //# sourceMappingURL=script.min.js.map comment; upload the map next to the script and browser dev tools will show your original code, with the original names, in stack traces and breakpoints. The map includes the original source, so do not publish it if the source must stay private.
Tips for smaller bundles
- Test the minified file. Code that relies on
Function.prototype.name,evalorwithcan behave differently after mangling; keep names or avoid those patterns. - Minify in your build with the same Terser options, so every release is minified the same way; use this page to compare settings.
- Need to read minified code? The JavaScript Beautifier restores indentation and line breaks.
- Optimize the whole page: minify markup and styles with the HTML Minifier and CSS Minifier, and compress images with the Image Compressor.
Frequently Asked Questions
Common questions about minifying JavaScript.
Paste the code into the left box. The minified version appears on the right with the bytes saved and a gzip estimate; copy it or download it as a .js file.
Terser 5, a widely used JavaScript minifier and the default minifier in webpack’s production mode. It runs in your browser.
Yes for normal code: Terser only applies transformations that keep the behaviour. Code that depends on function names, eval or exact toString() output may need “Keep function names” or should not be mangled.
No. The ECMAScript setting only controls which syntax Terser may use when it rewrites code. To run modern code in old browsers, transpile it with a tool such as Babel first, then minify.
Mangling renames local variables and function parameters to very short names. It saves a lot of bytes and does not change behaviour, because those names are not visible outside their scope.
Yes. With “Keep license comments”, comments starting with /*! or containing @license, @preserve or @copyright stay at the top of the output.
Tick “Source map”. The minified code then ends with a sourceMappingURL comment and a “Download .map” button appears; host both files in the same folder.
No. Terser is downloaded once from a CDN and runs in your browser, so your code never leaves your device.