JavaScript Minifier
Remove comments and whitespace from JavaScript to shrink it, then run the result before you ship it.
The tool reads code as strings, template literals, regular expressions, comments, words and symbols, and removes only whitespace and comments. Names are not shortened. Your code stays in the page, and it re-minifies half a second after you stop typing.
// sum
function add(a, b) {
return a + b;
}
| Step | Result | Size |
|---|---|---|
| Original | the four lines above | 45 bytes |
| Remove Comments, Remove Whitespace (default) | function add(a,b){return a+b;} | 30 bytes |
| plus Optimize Semicolons | function add(a,b){return a+b} | 29 bytes (36% smaller) |
A space survives only where removing it would glue two words together: the one in return a stays, the ones in a + b go. return a - -b becomes return a- -b so the two minus signs do not merge. Semicolon optimization removes only a ; right before }. Sizes are UTF-8 bytes.
What stays untouched
Quotes, backticks and regular expressions are copied exactly, so the // in "a // b" is not treated as a comment. Statements separated only by line breaks keep them, so output can span several lines. A /*! MIT */ license comment is deleted, so add it back afterwards.
A regex right after a closing parenthesis, as in if (x) /'/.test(s), is read as division and produces an Unterminated string error; put the regex in a variable. JSX fails because a closing tag's slash looks like a regex; convert it first. Large bundles are better handled by a build tool with source maps. For scripts inside HTML, minify here, then use the HTML minifier. Test the result.