ToolsHub4u

Strip comments and whitespace from JavaScript

Paste JavaScript to remove comments, indentation and repeated spaces. Tokens and line breaks are kept, so automatic semicolons still behave.

  • No renaming
  • Comments removed
  • Exact saving
Ready Developer & Utility
1Enter
2Adjust
3Copy result

JavaScript Minifier

1

JavaScript input

Options

Line breaks are never joined automatically. Automatic semicolon insertion depends on them, so removing them is left to a real build minifier.

No data yet Live
2

Minified JavaScript

Reduced 268 to 176 characters (34.3% smaller).

Characters saved

92

Minified JavaScript

  • Every code token is keptNo variable is renamed and no statement is removed. Strings, template literals and regex literals are copied with their contents.
  • Line breaks are preservedAutomatic semicolon insertion can depend on a newline, so the tool does not join lines. Remove blank lines only if that is what you want.
  • 2 comments foundThey were removed. Keep the original if a comment contains build instructions or a licence notice.
Before
268 chars
After
176 chars
Saved
34.3%
Comments found
2

This is a light minifier, not a replacement for Terser or esbuild. It does not rename variables, remove dead code, fold constants or drop whitespace inside expressions.

This page removes comments, indentation, blank lines and repeated spaces from JavaScript. It does not rename variables, join lines, fold constants or drop dead code. The result is shorter and still readable, which is a different goal from a production bundle minifier.

The caution is deliberate. Automatic semicolon insertion means a line break can be part of the program's meaning, so joining lines is one of the riskiest transformations a minifier can make. This page leaves line breaks alone and states that in its note. Use it to reduce a snippet, an inline script or a file you are about to review, not to replace a build step.

What the three options remove

Remove comments deletes both line comments and block comments. A licence header or a build instruction inside a comment is lost, so keep the original. The stats report how many comments were found, which is a useful check before you decide the output is safe.

Remove indentation and trailing spaces strips leading whitespace from every line and any spaces at the end. Lines start at the first character, so an indented function body loses its shape.

Remove blank lines deletes empty lines and lines containing only whitespace. Because line breaks between code lines are preserved, the output still has one statement per line where the input did.

Repeated spaces inside a line

A run of spaces or tabs between two tokens is now collapsed to a single space, and that space is dropped next to punctuation. const total = items.reduce(…) becomes const total = items.reduce(…), and const b = [ 1 , 2 , 3 ] ; becomes const b = [1,2,3];.

The rule is positional rather than semantic. A space is removed when the character before it is an opening bracket, brace, comma, semicolon, colon or dot, or when the character after it is a closing bracket, brace, comma, semicolon or colon. Everywhere else a single space stays, which is why return -1 and typeof x keep their separator.

A space before a dot is kept, because (1) .toString() and 1 .toString() need it: deleting the one after a number turns the call into a syntax error. Spaces inside strings, template literals, regular expressions and comments are never touched, so "a b" and a template containing three spaces come back exactly as written.

Removing an inline block comment no longer leaves two spaces behind. If /* note */ sat between two identifiers with no other separator, one space is inserted so the tokens do not merge, and the run of whitespace around it is collapsed to one.

Line breaks and automatic semicolon insertion

JavaScript inserts a semicolon at a line break in some situations, and the rule depends on what comes next. A minifier that joins lines without understanding the grammar can turn two statements into one expression or change a return followed by a newline into a function that returns nothing. This page avoids the whole class of problem by never joining lines.

The cost is that the output is not as small as it could be. A real minifier parses the program, joins lines safely, renames local variables to short names and removes unreachable code. It can cut a file by 60% or more. This page typically saves 15-35% on commented, indented source, and much less on code that is already compact.

If you need the aggressive behaviour, use a parser-based tool and run your tests afterwards. Do not paste the output of this page into production expecting bundle-size results.

A worked example

A 273-character function with a line comment, a block comment, indentation, repeated spaces and a blank line was minified with comments and indentation removed. The result was 174 characters, 36.3% smaller, with two comments removed.

Minification results for four inputs
InputOptionsResult
273-character functionComments and indentation174 characters, 36.3% smaller
Same functionComments, indentation, blank lines173 characters, 36.6% smaller
Same functionIndentation only, comments kept260 characters, 4.8% smaller
44,575-character script fileAll three options37,738 characters, 15.3% smaller, still parses

The all-options output kept every statement and every token. The comment above the function and the block comment before the if were gone, the indentation was removed, and the blank line between the function and the conditional disappeared. The code still has one statement per line and the same braces.

The run with comments kept is a useful comparison: it saved only 4.8%, because the input was already free of most extra whitespace. The large file is the more realistic figure for hand-written code with comments throughout, and its output was compiled by the browser to confirm that it still parses.

A space before a bracket is keptif (items.length) keeps the space before its bracket. Removing it is usually harmless, but the same edit on async (x) => {} would turn an async arrow into a call, so the tool stays conservative.

Comments, strings and regular expressions

Comment markers inside a string are not comments. A URL such as "https://example.com/a//b" contains // and a regular expression such as /https?:\/\/\//g contains escaped slashes. Both are copied intact, and only a real comment is removed.

Template literals are treated as strings, so a // or /* inside one is not removed either. A regex literal immediately after a division operator is the classic ambiguous case. This tokeniser also recognises return, typeof, case, in, of, new, delete, void, throw, else, do, yield and await as positions where a slash starts a regex, so return /a'b/.test(s) no longer lets the apostrophe open a string.

The tool does not shorten strings, change quote style or escape characters. Whatever was inside the quotes stays byte for byte. That makes the output safe to compare with the original once comments and whitespace are ignored.

What a build minifier does differently

A build minifier parses the file into a syntax tree, renames local bindings, removes unused code, folds constants and joins lines. It also produces a source map so a stack trace can point back to the original. This page produces none of that, and its output has no source map.

For a small inline script on a page, that difference rarely matters. For an application bundle, it does. Use the JavaScript beautifier to go back the other way when you need to read the code, and the code diff checker to confirm that only comments and whitespace changed.

Questions

Common questions

Short answers about this tool and its limits.

Does it collapse repeated spaces?

Yes. A run of spaces or tabs between tokens becomes one space, and the space is dropped next to brackets, braces, commas, semicolons, colons and dots. const b = [ 1 , 2 ] ; becomes const b = [1,2];. Spaces inside strings are untouched.

Does it rename variables or remove dead code?

No. Every code token is kept and no binding is renamed. It only removes comments, indentation, blank lines and extra spaces. That is why the saving is smaller than a build minifier's.

Why are line breaks preserved?

Because automatic semicolon insertion can depend on a newline. Joining lines without parsing the program can change behaviour. The tool leaves line breaks alone and tells you so.

Does it keep comments inside strings?

Yes. A // inside a string or a template literal is text, not a comment, and is copied intact. The same applies to regular expressions with escaped slashes.

Can it produce a source map?

No. It does not build a syntax tree, so it cannot map output positions back to the source. Use a build minifier if you need a source map for debugging.

How much smaller will my file get?

It depends on the input. The test snippet saved 36.3% and a 44,575-character script file saved 15.3%. Files with many comments and repeated spaces save more; already compact code saves little or nothing.

Does it change quote style or escape characters?

No. String contents and quote characters are copied as written. It does not normalise quotes, escape sequences or template literals.

Is my code uploaded?

No. Minification runs in the browser with no account or server copy. Uploaded files are read locally and capped at 5 MB. Save the result before closing the tab.