JSFormat JS Formatter

Stripping Comments From JavaScript Without Breaking It

JSFormat Guides · Updated 2026-10-31 · All guides

A regex such as /\/\/.*$/gm removes comments and also half of this line:

const url = "http://example.com/a";

// appears inside a string, a template literal and often a regex literal, and only a tokenizer knows which. The comment stripper uses the same tokenizer as the formatter, so those are left alone.

What it does

  • Removes // and /* */ comments, and HTML-style <!-- comments that engines still accept in scripts.
  • Replaces a block comment with a space (or a newline if it contained one), so a/**/b does not fuse into one token.
  • Optionally keeps licence comments: /*!, /*@, and anything containing @license or @preserve. Removing a licence header can be a legal problem, which is why keeping them is the default.
  • Trims trailing spaces left behind and collapses runs of blank lines.

How it checks itself

After removal the non-comment token stream must be identical to the input's. If the input compiled, the output must compile too. If either check fails the tool returns your code unchanged and says why.

What it does not do

It is not a minifier: it keeps indentation, line breaks and names. For smaller output use a real minifier (terser, esbuild), which parses the code and can shorten names. It also does not remove JSDoc that your documentation tooling needs, so keep a copy of the source.

Developer Sponsor / Partner
Copied to clipboard!