JavaScript Minifier

Strips comments and redundant whitespace from JavaScript without touching strings, template literals or regular expressions. Keeps semicolon-free code working and never renames variables.

The output will appear here.
Advertisement Kelivo — a word game by icyabmod, on Google Play and the App Store

How JavaScript Minifier works

Strips comments and redundant whitespace from JavaScript without touching strings, template literals or regular expressions. Keeps semicolon-free code working and never renames variables.

Input JavaScript
Output Minified JavaScript
Works done 3,893

How to use it

Paste a script and this tool will minify JavaScript by removing comments, indentation and redundant whitespace. It reads the code token by token, so strings, template literals and regular expressions are never altered, and line breaks that automatic semicolon insertion depends on are kept. Variable and function names are not shortened, which keeps the output recognizable when you debug it. Everything happens in your browser.

How to minify JavaScript here

  1. Paste the code into the input box, from a single function to a whole file.
  2. Press "Run tool" or Ctrl + Enter to minify it.
  3. Check the character and byte count under the output to see what you saved.
  4. Copy the result into your page or file, then run it once to confirm it behaves as before.

Scope: a safe minifier, not a full optimizer

  • Removed: line comments, block comments, indentation and spaces that carry no meaning.
  • Preserved: comments starting with /*!, which conventionally hold license notices, and a leading #! line.
  • Spaces that matter: kept where joining tokens would change them, as in a+ +b or typeof z.
  • Line breaks: joined only where it is provably safe for automatic semicolon insertion.
  • Not done: renaming (mangling), dead-code removal, constant folding, transpiling modern syntax and generating source maps. A build-time bundler does those; this page does not pretend to.

Worked example

A nine-line cartTotal(items, taxRate) function with a license header, two comments and normal indentation is 224 characters. After minification the header stays on its own line and the code becomes function cartTotal(items,taxRate){let total=0;for(const item of items){total+=item.price*item.qty;} followed by return total*(1+taxRate);} on a second line, 152 characters in all.

The line break before return is a deliberate safety margin. Joining it would be harmless in this case, but the minifier only merges lines when it can prove the meaning cannot change. You pay a byte; you never get a silently broken script.

Why semicolon-free code survives

JavaScript inserts semicolons at some line breaks on its own, and naive minifiers that glue every line together break that. Here is how this one handles the classic traps:

SourceOutputWhy
let a = 1 / let b = 2 on two lineslet a=1 / let b=2, still two linesJoining would produce a syntax error
return / 42 on two linesreturn / 42Joined, the function would return 42 instead of undefined
i / ++ / jLine breaks keptThe increment belongs to j, not i
'Hello world' and /a b/gUnchangedSpaces inside strings and regexes are data

One case surprises people: a line ending in const c = a followed by a line starting with (b) becomes const c=a(b). That is not the minifier's doing; JavaScript already parsed the source as a function call. If a line must start with a parenthesis or bracket, put a semicolon in front of it.

When to use this instead of a build tool

For an application, a bundler that renames variables and removes unused code will save more. This page is for quick jobs without a toolchain: a custom script field in a CMS, a small widget embedded in a landing page, a snippet sent to a client, or a bookmarklet. For bookmarklets, write every semicolon explicitly: browsers strip line breaks from URLs, so any break the minifier keeps for safety would be lost. Background reading: what is minification.

Privacy and related tools

Minification runs in JavaScript in your own tab; your code is not sent to the server, and the page only increments an anonymous usage counter. The tool does not validate syntax, so broken input produces broken output. To read minified code, use the JavaScript beautifier; for pages with inline scripts, the HTML minifier applies the same rules to every script block.

Advertisement Clicker Alchemist — a mobile game by icyabmod, on Google Play and the App Store

Frequently asked questions

It should not, provided the input is valid. Only comments and meaningless whitespace are removed; strings, template literals and regular expressions are untouched, and line breaks that automatic semicolon insertion relies on are preserved. Names are never changed. The tool does not check syntax, though, so it is still good practice to run the minified file once before you ship it.

Safe renaming requires analyzing the whole program: which names are global, which are used by other scripts on the page, and which are exported. A single pasted snippet does not provide that context, so renaming could break functions called from HTML attributes or other files. This tool avoids that risk entirely. If you want the extra savings of mangling, let a bundler do it at build time, where it sees the whole codebase.

It depends on how much of the file is comments and indentation. Heavily commented, well-indented source can shrink noticeably, as in the 224 to 152 character example above, while code that is already compact will barely change. Because names are not shortened, the result is larger than what a renaming minifier produces. Server compression such as gzip or Brotli shrinks the transferred size further in both cases.

Yes. Block comments that begin with /*! are preserved in place, because that is the common convention for copyright and license notices that open-source licenses ask you to keep in distributed copies. A leading hashbang line is kept too. Every other comment, including TODO notes and commented-out code, is removed. Use the /*! form for any notice you need to survive minification.