What this JavaScript minifier does
This tool runs Terser, a widely used JavaScript minifier, directly in your browser. It actually parses your code, so it knows the difference between a string, a regular expression, a comment and real code, which simple whitespace strippers get wrong.
There are two modes. Whitespace and comments only removes comments and unneeded spacing and puts everything on one line, but keeps every variable and function name as written. Full also shortens local variable names, merges declarations and simplifies expressions for the smallest result. Names that other code might call, such as top-level functions, are kept in both modes.
When it is handy
It suits quick jobs: shrinking a snippet for a bookmarklet, fitting code into a place with a length limit, preparing a small inline script, or seeing how much of a script is comments and spacing. The size before and after, and the percentage saved, are shown above the result.
For a whole project, use the same kind of minification in your build tool so it runs automatically on every release. If you are working with styles instead, the CSS Minifier does the equivalent job, and for data the JSON Minifier is exact.
How to use it
- Paste your JavaScript into the box labeled JavaScript. A small function with a comment is already there.
- Pick a mode: whitespace and comments only, or full.
- The Minified box updates a moment after you stop typing. Click Copy to copy the result.
If your code has a syntax error, the tool shows the message with the line and column instead of output, which also makes it a quick syntax check. Your code is processed in the browser and is never uploaded. The Terser engine downloads the first time you use this tool, so the first result can take a second.
What is kept intact and what to check
Because the code is parsed, strings keep their spaces, regular expressions are not mangled, and code that relies on automatic semicolon insertion gets proper semicolons added instead of breaking.
Things to keep in mind: the tool handles modern JavaScript syntax but not TypeScript or JSX, so compile those first. In full mode, anything that depends on a function's name or on reading a variable name from a string (such as eval or code that inspects Function.name) can behave differently. In every case, run the minified code before relying on it, and keep your readable source as the file you edit.