How to use the javascript minifier
- Paste your JavaScript source into the text area above.
- The minified output appears instantly as you type.
- Check the stats below the output for original size, minified size, bytes saved and a rough gzip estimate.
- Copy the result into your build output or deployment.
How this javascript minifier works
A javascript minifier reduces file size by stripping characters the engine does not need. This tool uses a tokenizer that recognises strings, template literals, regular expressions and comments. It drops single-line comments starting with // and block comments wrapped in /* */, then removes whitespace between tokens, keeping a single space or a newline only where removing it would change the meaning.
For example, the input var x = 1; // set x becomes var x=1;. The tokenizer sees the // outside any string, discards everything until the newline, and removes the spaces around the equals sign.
What this minifier does and does not change
The tool only removes comments and whitespace. Identifiers are not renamed, semicolons are not added or removed, and expressions are not rewritten, so the program means the same thing after minification. Comments that start with /*! or contain @license or @preserve are kept so licence headers survive.
Where two lines could be read as one statement or where a line break matters for automatic semicolon insertion, the tool keeps a newline instead of joining them. That makes the output slightly larger than a full compressor, which renames variables and rewrites code, but it is safe for code written without semicolons. For other formats, use the dedicated css minifier or json minifier instead.
Why minify JavaScript
Smaller files download faster. A well-commented script often shrinks noticeably after comments and indentation are removed, and gzip compression on the server reduces it further. When you use a minifier js tool before deployment, you lower bandwidth costs and improve page load times.
Minification also removes developer-facing comments that could expose internal logic. Combined with bundling, it keeps production assets lean.
Common pitfalls when minifying
Not all whitespace is optional. Removing the space between return and an object literal on the next line triggers automatic semicolon insertion, changing the function's behaviour. This tool keeps the line break after return, so the code behaves exactly as written.
Regular expressions are another trap. The slash character / starts both division and regex literals, and confusing the two leads to broken output. This tool looks at the previous token to decide which one it is.
Template literals using backticks can span multiple lines and contain embedded expressions. Collapsing newlines inside a template literal would alter the string value, so the tokenizer leaves their contents untouched.
Understanding the size statistics
After minification, the tool shows four metrics:
| Metric | Meaning |
|---|---|
| Original size | UTF-8 byte length of the input |
| Minified size | UTF-8 byte length of the output |
| Bytes saved | Difference between original and minified, with percentage |
| Gzip estimate | A rough figure, calculated as about 35 percent of the minified size; real gzip output varies with the code |
Treat the gzip figure as a ballpark only. Your server or CDN determines the real transfer size.
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple function | JavaScript source: // add two numbers / function add(a, b) { / return a + b; / } / | Original size: 58 bytes; Minified size: 30 bytes; Bytes saved: 28 bytes (48.3%); Gzip estimate: ~20 bytes |
| String with spaces | JavaScript source: const msg = "hello world"; // keep inner spaces / console.log(msg); / | Original size: 68 bytes; Minified size: 43 bytes; Bytes saved: 25 bytes (36.8%); Gzip estimate: ~20 bytes |
| Multi-line comment block | JavaScript source: /* / * Block comment / */ / let x = 10; / | Original size: 36 bytes; Minified size: 9 bytes; Bytes saved: 27 bytes (75%); Gzip estimate: ~20 bytes |
FAQ
Is this js minifier safe for production code?
Yes for ordinary code. It only removes comments and unnecessary whitespace. It does not rename variables or restructure expressions, so the runtime behaviour stays the same. Always run your tests on the minified file before you deploy it.
Does this js online minifier upload my code?
No. Everything runs locally in your browser using JavaScript. Your source code never leaves your device.
What is the difference between a js minifier and a javascript beautifier?
A minifier shrinks code by removing formatting, while a beautifier expands it for readability. You can use the javascript beautifier to reverse minified output.
Can I use this minifier js tool for TypeScript?
This tool targets standard JavaScript syntax. TypeScript type annotations are not understood, so compile TypeScript to JavaScript first, then minify the result.
How much size does minification typically save?
Removing comments and whitespace typically cuts a noticeable share of uncompressed source, depending on how heavily it is commented and indented. Gzip or Brotli on the server then compresses the result further.
Should I minify before or after bundling?
Most build pipelines bundle first, then minify the combined output. This avoids redundant work and lets a full minifier optimise across module boundaries. This tool is for single files and quick jobs.