Skip to content
FileKit
Developer ToolsRuns in your browser6 min read

JavaScript Minifier

This javascript minifier removes comments and collapses unnecessary whitespace from a JavaScript file without touching the logic of the code. Paste a script or drop a .js file, and the tool returns a smaller version with the same behaviour, safe to ship because every character it removes was never meaningful to the JavaScript engine in the first place.

Loading the tool interface
Files never leave your deviceNo upload wait and no queueNo signup, watermark or file limit

Say plainly what this javascript minifier is not: it is not a full compressor like Terser or UglifyJS. Those tools rename local variables, restructure control flow and fold constants, and doing that safely requires a full parser that builds an abstract syntax tree of the entire program. This tool is a character scanner. It reads the source one character at a time, keeping track of whether it is inside a string, a template literal, a regular expression or a comment, and removes only the comments and the whitespace that sit outside all of those. That is a smaller job than a real compressor does, and it is honest about the size it gives up in exchange for never corrupting a working file.

Everything happens in your browser. Source code, including code that has not shipped yet, is read and rewritten locally with no upload and no server round trip, so this remains a private way to minify javascript online during development or before a manual deploy.

What this javascript minifier removes, and what it deliberately leaves alone

It removes single line comments starting with two slashes, multi line comments wrapped in slash star, and it collapses runs of spaces, tabs and blank lines down to the minimum whitespace needed to keep tokens separated. That alone typically cuts 15 to 30 percent off a file that has not already been minified, since comments and formatting whitespace carry zero runtime meaning. If your only goal is to remove javascript comments before sharing a file, the comment toggle alone does that safely without touching whitespace at all.

It leaves identifiers, function names and property names completely untouched, so a stack trace from production still shows the real function name instead of a single letter. It leaves the structure of the code untouched too. No statement is reordered, no variable is renamed and no dead code is removed, because all of those transformations require understanding what the code actually does, not just where the comments and extra spaces are.

  • Removes // line comments and /* block */ comments outside strings and regex literals.
  • Collapses multiple spaces, tabs and blank lines to the minimum needed to separate tokens.
  • Never alters a string, a template literal or a regular expression literal, including their contents.
  • Never renames a variable, folds a constant or reorders a statement.
  • Preserves line breaks after certain tokens so automatic semicolon insertion still behaves correctly.

The scanner: how it tells a comment from a string that contains slashes

A regular expression based find and replace is the naive way to strip comments, and it is also the way naive minifiers corrupt working code. The text "http://example.com" inside a string contains two slashes that look exactly like the start of a line comment to a regex that is not tracking context, and a template literal like `${a / b}` contains a division that looks exactly like the start of a regex literal to a scanner that only looks at the current character.

This javascript minifier avoids that by walking the source character by character and maintaining a small state machine: it knows at every position whether it is inside a single quoted string, a double quoted string, a template literal, a line comment, a block comment, or plain code, and it treats a slash differently depending on which of those states it is in. Inside any string or template literal, nothing is touched, including characters that would otherwise look like the start of a comment. That is the single design decision that keeps this tool from producing broken output on real world code.

The one ambiguity every javascript minifier has to resolve: division or regex

A forward slash in JavaScript starts either a division operator or a regular expression literal, and the language itself decides which one based on the token that came immediately before it. After an identifier, a number, or a closing parenthesis or bracket, a slash is division. After most operators, an opening brace, a comma, or certain keywords like return, a slash starts a regex literal.

The scanner in this tool tracks the last meaningful token it saw for exactly this reason, so `a / b / c` is parsed as two divisions while `return /pattern/g` is parsed as a regex literal followed by its flags, and neither case is mistaken for a comment even though both contain a slash. Getting this wrong is the most common way a hand written minifier silently breaks a file that uses regular expressions, so it is treated as the highest priority case in the scanner.

When to use a real compressor instead of this javascript minifier

For a production web app served to real users, a build step running Terser or the minifier built into esbuild or SWC will typically produce a file 30 to 50 percent smaller than this tool, because those tools rename local variables to single letters, remove genuinely dead code and fold constant expressions, all of which requires a full parser and a build pipeline, not a browser tab.

This tool is the right choice for a quick pass on a script before pasting it somewhere with a character limit, cleaning up a one off utility file, or removing comments from code before sharing it publicly without a build step in the loop. It is a js file size reducer for exactly those situations, not a replacement for a production bundler.

How this javascript minifier works under the hood

The tool is a single pass character scanner written specifically for this page. It never builds an abstract syntax tree, never evaluates the code and never executes anything you paste into it. That is both what keeps it safe to run on any script, since nothing can be triggered by malicious input, and what limits it to the class of transformations it can prove will never change behaviour: removing comments and collapsing whitespace outside of strings, template literals and regex literals.

Because there is no parser and no execution, the same character by character approach also makes it fast. A javascript whitespace remover built this way processes a multi megabyte file in a fraction of a second directly in the browser tab, with no upload and no queue.

How to minify JavaScript

  1. 1

    Add your JavaScript

    Paste a script into the input box, or drop a .js file onto the page. Nothing is uploaded.

  2. 2

    Choose what to strip

    Toggle comment removal and whitespace collapsing independently depending on what you need.

  3. 3

    Minify

    Run the scanner and review the before and after size in the stats panel.

  4. 4

    Copy or download the result

    Copy the minified script to your clipboard or download it as a .js file.

Related tools worth bookmarking

Sources and further reading

Frequently asked questions

Common questions about the javascript minifier.

No, and this page says so deliberately. A full compressor renames variables, removes dead code and folds constants using a complete parser, which typically saves 30 to 50 percent more than comment and whitespace removal alone. This tool is a safer, simpler character scanner for quick jobs outside a build pipeline, not a replacement for a production bundler.

The scanner tracks whether it is inside a string, a template literal, a regex literal or a comment at every character, so text that looks like a comment inside a URL string or a division that looks like a regex start is never touched incorrectly. This is exactly the class of bug that a naive regex based approach is prone to and this tool is built to avoid.

No. Only comments and whitespace outside of strings, template literals and regex literals are removed. Every identifier, every string value and every statement stays exactly as written, so the minified file runs identically to the original, just with a smaller file size.

No. The scanner runs entirely in your browser using plain JavaScript with no network request involved. Your source code, including anything unreleased or private, is never transmitted, stored or seen by anyone but you.

Comment and whitespace removal alone typically saves 15 to 30 percent, since it cannot rename variables or remove dead code the way a full compressor can. If you need a larger reduction for a production deployment, run the output through a build tool such as Terser, esbuild or SWC as an additional step.

Yes. The scanner maintains state for single quoted strings, double quoted strings, template literals and regex literals separately, and it uses the preceding token to decide whether a slash starts a regex or represents division, which is the same rule the JavaScript language itself uses.