JavaScript Minifier
Compact JavaScript by removing ordinary comments and extra whitespace.
Your result will appear here…
How to use JavaScript Minifier
- Paste your JavaScript.
- Select Minify.
- Run your own tests on the result before you ship it.
Example
Input / settings:
function greet(name) {
// Say hello
return `Hello, ${name}!`;
}Output:
function greet(name){return`Hello, ${name}!`;}The comment and the whitespace around the code are removed. Text inside the template literal, including the space after the comma, is kept exactly.
When to use
- Shrink a small script before you embed it in a page.
- See how much of a file is comments and whitespace.
- Compare a function with its compact form.
Overview
JavaScript Minifier removes comments and whitespace from JavaScript with a lightweight tokenizer, not a parser. It reads strings, template literals, regular expressions and numbers as single pieces, keeps the line breaks that automatic semicolon insertion depends on, and keeps the spaces the language requires. It does not rename variables, remove unused code or check that the program is valid.
Supported input
Up to 200,000 characters of JavaScript. Modules, classes and async functions pass through as ordinary code. JSX is not supported and TypeScript annotations are kept as text.
Output details
Compact JavaScript. Comments are removed except /*! comments, and line breaks that automatic semicolon insertion depends on are kept.
Common mistakes
- Line breaks are not always removed. Code written without semicolons keeps them: let a = 1 followed by let b = 2 stays on two lines, and return followed by a line break keeps it, because removing it would change what the code does.
- Variable names stay as written and no code is removed, so the saving comes only from comments and whitespace. In one test run the output was about 66% of the size of acorn and 69% of React's development build.
- A / after a closing brace is read as division. After function f(){}, the regular expression statement /a +b/.test(s) loses the space in its pattern and becomes /a+b/, which is a different pattern. Put a semicolon in front of such a statement.
- JSX is not understood: <div>don't</div> is rejected with A string is not closed, because the apostrophe is read as the start of a string. TypeScript annotations pass through unchanged as ordinary text.
- Unreadable text is rejected rather than guessed. An unclosed string, template literal, comment or regular expression returns an error and no output.
- Every comment is removed except those that start with /*!. That includes //# sourceMappingURL lines, so the output no longer points to a source map.
Practical tips
- The tool never runs your code, so nothing here proves it still works. Run your tests on the minified output.
- Write notices that must survive as /*! license */ comments.
Privacy and security
This tool runs entirely in your browser. Your input is never uploaded to our servers. Minification runs locally in your browser and nothing is uploaded. Your code is only treated as text and is never run. Minified code is not obfuscated or protected, and anyone can format it again with JavaScript Beautifier.
References
- MDN: Lexical grammar
- ECMAScript Language Specification: Automatic Semicolon Insertion
- MDN: Template literals
Frequently asked questions
Does JavaScript Minifier rename variables or remove dead code?
No. Names and statements are written back as they were. Only comments and unneeded whitespace change.
Why did my minified code keep some line breaks?
JavaScript can end a statement at a line break when a semicolon is missing. The minifier keeps those breaks so the code means the same thing.
Can it minify modules, classes and async functions?
Yes. import and export, classes, class fields and async functions pass through like any other code. The tool does not check that they are valid.