Strip comments from JavaScript, keeping licences and JSDoc if you want

Paste JavaScript to remove its line and block comments. Strings such as "https://…" and regular expressions are left exactly as written, and licence or JSDoc comments can be kept.

Live demo · real result from this tool

Code without comments

/*! my-lib v1.0 | MIT */function add(a, b) {  return a + b;}const url = "https://example.com/a//b";const re = /\/\/+/g;
Comments removed4
Size before
234 B
Size after
124 B

Processed on your device — nothing is uploaded.

How to use the JS comment remover

  1. Paste your code into the JavaScript box.
  2. Tick Keep JSDoc (/** … */) comments if documentation blocks should stay.
  3. Leave Keep licence comments (/*! … */) ticked unless you are sure the notice is not required.
  4. Keep Collapse repeated blank lines on to squeeze the gaps left by removed comments into a single blank line.
  5. 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.io and 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-disable or /* 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?
No. The code is split into tokens first, so the // in "https://example.com" is part of a string and the slashes in a regex such as /\/\/+/g are part of the regex. Only real comments are removed.
What counts as a licence comment?
A block comment that starts with /*! or contains @license or @preserve. These are kept by default because many open-source licences require the notice to stay with the code.
Why are the sizes different from my file size?
Size before and Size after count characters in the text box, so a file with many non-ASCII characters or Windows line endings may differ slightly from its size on disk.
Can I use it on JSX or HTML?
Mostly. The lexer does not understand JSX text, but a // glued to a URL scheme (http://x.io) or followed by a closing tag on the same line is kept as text. Plain text such as a // b between tags can still be read as a comment. For HTML files use the HTML comment remover instead.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close