How to use the JS comment remover
- Paste your code into the JavaScript box.
- Tick Keep JSDoc (
/** … */) comments if documentation blocks should stay. - Leave Keep licence comments (
/*! … */) ticked unless you are sure the notice is not required. - Keep Collapse repeated blank lines on to squeeze the gaps left by removed comments into a single blank line.
- Copy the Code without comments, and compare Size before and Size after.
What it does and when to use it
Comments are useful while you write code and noise when you publish a short snippet, paste code into a question, or ship a small script without a build step. Use this tool to remove them while keeping the parts that matter: licence headers and, if you choose, the JSDoc blocks that editors read for type hints.
To go further, the JavaScript minifier also renames variables and removes whitespace. The console.log remover clears debug output in the same careful way. For markup, use the HTML comment remover.
How it works
A lexer reads the code once and labels each piece as code, string, template literal, regex or comment. Telling a regex such as /\/\/+/g apart from division is the hard part, and the lexer decides from the token that comes before the slash. Each comment token is then dropped unless one of the keep options matches it. A line that held only a comment disappears completely; a comment after code is removed along with the spaces before it. When a block comment sat between two pieces of code with no space, a single space is put in its place so the tokens do not join together.
Worked examples
Default settings.
/*! my-lib v1.0 | MIT */
/**
* Adds two numbers.
* @param {number} a
*/
function add(a, b) { // quick add
return a + b; /* inline */
}
const url = "https://example.com/a//b"; // the // inside the string stays
const re = /\/\/+/g;
becomes:
/*! my-lib v1.0 | MIT */
function add(a, b) {
return a + b;
}
const url = "https://example.com/a//b";
const re = /\/\/+/g;
Four comments were removed and the size fell from 234 B to 124 B. The licence line stays, the URL and the regex are untouched.
Keep JSDoc, drop the licence. With Keep JSDoc ticked and Keep licence comments unticked, the same function keeps its /** Adds two numbers … */ block and loses the /*! … */ header.
No accidental joins. let total = price/*tax*/*qty; becomes let total = price *qty;. The space stops / and * from merging into a new comment.
Limits and tips
- Commented-out code goes too. If a comment holds code you still need, uncomment it first.
- JSX text is read as code. URLs such as
http://x.ioand a//followed by a closing tag on the same line are kept, but other//text between tags is treated as a comment. - Directives in comments, such as
// eslint-disableor/* webpackChunkName */, are removed like any other comment. - Indentation is kept as it was; run the JavaScript formatter afterwards if you want it tidied.
Frequently asked questions
Will it break URLs or regular expressions?
What counts as a licence comment?
Why are the sizes different from my file size?
Can I use it on JSX or HTML?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.