JavaScript Minifier

Paste a script and the tool removes the comments and every space or line break that the language does not need, then shows how many bytes you saved. It reads the code the way a browser does, so strings, template literals and regular expressions are never touched, and line breaks that automatic semicolon insertion depends on are kept. It does not rename variables or rewrite your logic, which makes it safe to use on code you cannot test everywhere.

What you enter

What you get

How it works

  1. Paste your JavaScript into the box. Nothing is uploaded; the work happens in your browser.
  2. Choose whether to keep comments that start with /*! or contain @license or @preserve. Keeping them is polite when the code is someone else's.
  3. Read the sizes: bytes before, bytes after and the percentage saved. The minified code appears below.
  4. Copy the result or download it as a .min.js file, then test it in the page where it will run.

What minifying does and does not change

A minifier is a one-way transformation: the output runs the same as the input but is not meant for people to read. The safest minifiers work on whitespace and comments, because the grammar of the language says exactly where those are optional. Deeper optimisations such as renaming local variables or removing unused functions can save more, but they rely on assumptions about how the code is used, for example that no one calls a function by its name from a string.

The risky places for whitespace removal are the spots where a space or line break separates two tokens that would otherwise join into one. The words return and value must stay apart, and a plus followed by another plus must not turn into an increment. The minifier checks each boundary and keeps a single space where two neighbouring characters would fuse.

Where minifying fits in a site build

Minifying is one of several things that make a page load faster, along with compression on the server, caching and loading scripts only on the pages that need them. For a small script on a simple site, it is a quick win. For a large app, the build tool handles it for you, and a standalone tool like this one is for the odd file that is outside the build.

After minifying, check the browser console for errors on a page that uses the script. If something breaks, compare the result with the original around the failing line. Most problems come from code that relied on automatic semicolon insertion in a way a human reader would not notice.

Tips

Frequently asked questions

What does a minifier remove?

It removes comments, indentation, spaces around punctuation and line breaks that have no meaning. For example, var total = a + b; becomes var total=a+b;. The program does exactly the same thing, but the file is smaller and downloads faster.

Is it safe for my code?

The tool tokenizes the code first, so text inside strings, template literals and regular expressions is copied exactly. It keeps a line break wherever removing it could change how the code is read, such as after return, before a ++ or between two statements that have no semicolon between them. Test the result in your page, as with any build step.

Why are some line breaks still in the output?

JavaScript can end a statement at a line break when you leave out a semicolon. If the tool removed every line break in such code, two statements could merge into one and break. It removes the ones that are clearly safe and keeps the rest.

Does it rename variables or shorten names?

No. Renaming needs a full understanding of scope, and mistakes there change behaviour. This tool leaves every name as you wrote it.

How is the saving calculated?

Both sizes are counted in bytes as UTF-8, which is the size sent over the network. The saving is the bytes removed divided by the original size, shown as a percentage. If the original is 1,000 bytes and the result is 700, the saving is 30%.

Related: JSON formatter and validator, Image compressor, Regex tester.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Free Tools

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap