DEVELOPER TOOL

JavaScript Minifier

Paste JavaScript to strip comments and extra whitespace. For light cleanup — not a substitute for a full build-step minifier on production bundles.

0 B
Original
0 B
Minified
0%
Reduced

A note on this tool

This is a basic whitespace-and-comment minifier: it strips comments and collapses whitespace while leaving strings and template literals untouched and keeping line breaks where automatic semicolon insertion could depend on them. It does not fully parse JavaScript, so unusual code (for example, some regex literals) can still be mishandled — always test the output. It does no variable renaming or dead-code elimination; for production code, use a real parser-based minifier such as Terser.

How to use the JavaScript Minifier

  1. Paste your JavaScript into the input box.
  2. Click Minify to strip comments and collapse whitespace.
  3. Test the output before using it, especially for scripts with regex literals or complex template strings.

Common use cases

Worked example

Paste in a small utility function written with generous spacing and inline comments explaining each step, and minifying strips the comments and collapses the whitespace down to a single dense line — for a typical 40-line helper function, that's often a 30–50% size reduction from formatting removal alone, before any variable renaming or dead-code elimination that a full build-tool minifier would additionally apply.

Things to watch out for

This tool performs basic, surface-level minification (removing comments and collapsing whitespace) rather than the more aggressive transformations a build-tool minifier like Terser or UglifyJS performs — those tools also rename local variables to shorter names and can eliminate genuinely unreachable code, producing a meaningfully smaller file for a large production bundle. For a quick one-off script, or code with no build pipeline attached to it at all, this level of minification is enough; for a production JavaScript bundle serving real traffic, route it through a proper build step instead. Always keep the original, readable source as your file to edit — never edit the minified output directly, since a single missing semicolon in dense minified code is far harder to spot and fix.

Code that breaks when minified: our tests

We ran small snippets through the tool's exact rules and executed the before and after versions in Node.js. These patterns produced broken output:

Changes inside strings

Some output runs but behaves differently. Spaces next to = , : ; ( ) { } and the operators are removed everywhere, including inside quotes, so console.log("a = b, c") prints a=b,c after minifying. Template literals lose their line breaks and indentation: a two-line template string becomes one line joined by a single space. If your code builds messages, HTML or SQL from strings, compare the output carefully.

How to check the output quickly

  1. Save the result as a file and run node --check file.js. Node.js parses the file without running it and reports the first syntax error with its position.
  2. Load the page that uses the script and open the browser console. A syntax error stops the whole file, so one error is enough to break every feature in it.
  3. Exercise anything that prints text, because string changes do not cause errors.

Code written with a semicolon at the end of every statement, with no // inside strings and no spaced-out + + or - -, ran the same before and after in the cases we tested. For anything else, a parser-based tool such as Terser is the safer choice. For other quick jobs that run in the browser, see our roundup of browser tools for developers.

Frequently asked questions

Is this safe to use on production JavaScript?

Not without testing. This is a basic whitespace/comment minifier — it doesn't fully parse the code like a real minifier, so some unusual syntax can be mishandled. For production code, use a parser-based minifier like Terser that understands JS syntax fully.

Why might minifying break code with regex literals or template strings?

Because this tool uses a simple character scanner rather than a full JS parser, it has to guess whether a / starts a regex literal or is a division sign, and it can guess wrong in unusual code (for example, a regex right after a closing parenthesis). Always test minified output before deploying it.

Does minifying rename variables to save more space?

No — this tool only removes comments and unnecessary whitespace. Variable renaming requires full parsing and is left to dedicated minifiers.

Why does my minified JavaScript say Unexpected identifier?

Most often the source relied on line breaks instead of semicolons. This tool joins all lines with spaces, so two statements that were on separate lines end up side by side with nothing between them. Add a semicolon at the end of each statement in the original, then minify again.

Does the JavaScript Minifier keep license comments?

No. Every block comment is removed, including the /*! ... */ style that many build tools keep for license notices, and so is every line comment. If a library license requires its header to stay with the code, paste the header back at the top of the minified output.

Are URLs in my code safe when minifying?

A URL such as https://example.com is kept, because the tool ignores // when it follows a colon. A URL containing a second double slash, like https://example.com//path, is cut off at that point and breaks the string. Check any string that contains // after minifying.