Coding Tools

JavaScript Minifier

Use this free JavaScript Minifier to strip comments and unnecessary whitespace from JS files and reduce their size. Read the guidance below on what minification does and does not change, since the difference between a safe minifier and an aggressive one matters more than the percentage saved. No sign-up required.

Instant Minification Runs in Browser 100% Free

Why You Are Here

A script is larger than it needs to be. A plugin, a snippet, or a file you inherited, shipping comments and formatting the browser never needs.

You have no build pipeline. A static site, a CMS theme, or a landing page where nobody is running a bundler and a web tool is the practical option.

PageSpeed has told you to minify JavaScript. A common audit recommendation, and worth understanding before acting on.

What This Tool Does

The JavaScript Minifier removes comments and unnecessary whitespace from JavaScript, producing a smaller file that behaves identically.

Paste your code or upload a file, and the tool returns the minified result ready to copy or download.

Everything runs in your browser. No code is uploaded or stored.

Two Kinds of Minifier, Two Levels of Risk

This distinction matters more than anything else here, and most tools do not make it.

 

Whitespace-only

AST-based

Removes comments and spacing

Yes

Yes

Renames variables

NO

Yes

Rewrites expressions

NO

Yes, true becomes !0

Removes unreachable code

No

Yes

Typical saving

Moderate

Substantially larger

Risk of behaviour change

Very low

Low but real

Needs a full parser

No

Yes

Whitespace-only minification is close to risk-free

It deletes characters the engine ignores. Strings, template literals, and regular expressions are left untouched, so the program that comes out is the program that went in.

AST-based minification parses your code into a syntax tree, transforms it, and regenerates it

That is where the large savings live, since renaming local variables to single characters is the biggest single win available, and also where the edge cases live.

Neither is wrong

Which you want depends on whether you have a build pipeline that can test the output, and how much you need the extra bytes.

Automatic Semicolon Insertion - The Real Hazard

This is where naive minifiers corrupt working code, and it is worth understanding whichever tool you use. JavaScript inserts semicolons for you at certain line breaks. The rules are specific, and they mean a line break can carry meaning.

function f() {

  return

  42

}

This returns undefined, not 42

ASI places a semicolon after return, so the 42 is unreachable. A minifier that joins those lines produces return 42, which returns 42. Same input, different program, no error message anywhere.

Line breaks matter wherever the previous token could end a statement and the next could begin one

That is exactly the condition under which ASI fires, and it is why a minifier must either preserve those breaks or parse the code properly enough to insert the semicolons itself.

Semicolon-free code is not the problem

Standard style without semicolons is perfectly valid and survives a correct minifier intact. The problem is a minifier that treats newlines as free whitespace when sometimes they are not.

A note on the debate: whether to write semicolons is a style question people argue about endlessly. It is not really a minification question, because a correct minifier handles both. It only becomes a minification question when the minifier is wrong.

What Mangling Actually Does

If you use an AST-based minifier, variable renaming is where most of the saving comes from.

// Before

function calculate(firstNumber, secondNumber) {

  const result = firstNumber + secondNumber;

  return result * 2;

}


// After mangling

function calculate(a,b){return 2*(a+b)}

Local variables are renamed. Some things are not

Element

Mangled by default?

Local variables and parameters

Yes

Function names

Often preserved, may be public API

Object property names

NO — would break property access

Global identifiers

No

Strings

No

Property names are excluded for a good reason

Renaming a property to a single letter breaks anything accessing it by string, including JSON.parse output, bracket notation access, and any external code touching your objects. Minifiers that offer property mangling require an explicit reserved list, and it is easy to get wrong.

Constant folding happens too

An expression like 100 times 2 becomes 200, large numbers become scientific notation, and true becomes an exclamation mark followed by zero, all evaluated at build time rather than runtime.

Keeping function names is worth considering

Production error stacks are considerably more useful when function names survive, and the byte cost is small.

Your Real Saving Is Smaller Than the Percentage

An honest framing that headline figures obscure. Minification percentages are quoted on raw file size. A file dropping from 100 KB to 60 KB is reported as 40% smaller.

But your server compresses before sending

Gzip and Brotli both work by finding and encoding repetition, and whitespace, indentation, and repeated identifiers are exactly the kind of repetition they handle well.

Stage

What it removes

Minification

Characters the engine ignores

Compression

Repetition across the whole file

Overlap

SUBSTANTIAL

So the meaningful comparison is minified-and-compressed against unminified-and-compressed

And that gap is much narrower than the raw figure.

Minification still helps

Shorter identifiers mean fewer distinct symbols for the compressor to encode, and less code for the browser to parse. Parsing is real work on mobile devices. But the benefit is smaller than a raw percentage implies.

Worth checking your server actually compresses

If compression is not enabled, enabling it is a far larger win than minifying. It is a bigger lever than anything else on this page, and it is a configuration setting rather than a build step.

Minification Is Not Obfuscation

Mangled code is not protected code

Any beautifier reverses the formatting in seconds, and while variable names are lost, the logic is entirely readable.

 

Minification

Obfuscation

Purpose

Smaller files

Harder to understand

Reversible

Formatting, yes

Deliberately resistant

Performance cost

None

Frequently significant

Protects your code

NO

Somewhat, briefly

Anything shipped to a browser is readable

If a secret must stay secret, it belongs on a server. No client-side transformation changes that, and treating minification as a security measure is how API keys end up in public bundles.

Source Maps

Minified code is undebuggable without them, and they carry a consideration people miss.

A source map maps minified output back to your original source

So browser dev tools show real file names, line numbers, and variable names in stack traces. Without one, a production error reports a position in a single-line minified file, which tells you almost nothing.

The consideration

A publicly accessible source map exposes your original, commented, unminified source to anyone who looks. That is fine for open code and a disclosure for proprietary code.

Two common approaches

Publish source maps for open projects, or upload them to your error tracking service without serving them publicly, which gives you readable stack traces without publishing the source.

When to Use a Web Tool and When Not To

Situation

Better approach

One-off file or snippet

This tool

Static site with no build step

This tool

CMS theme or plugin file

This tool

Project with a bundler

Minify in the build

Anything version controlled

Minify in the build

Code needing source maps

Minify in the build

If you have a build pipeline, minify there

It runs automatically, produces source maps, and keeps your source file as the thing you edit. Pasting into a web tool means the minified file is what you have, and re-editing it later is painful.

Never edit minified code

Keep the original, change that, and minify again. A minified file is an output, not a source.

Common Mistakes to Avoid

Overwriting Your Source File With Minified Output

Minification is one-way in practice:
a beautifier restores formatting but cannot restore comments or original variable names if mangling was applied. Once the minified version is all you have, every future change means working in unreadable code. Keep the original, save the minified output alongside it as a separate file, and treat that file as a build artefact rather than something to edit.
Assuming Minification Protects Your Code

Mangled JavaScript is not obfuscated and obfuscated JavaScript is not secure. Anything delivered to a browser can be read, reformatted, and understood, and variable names are the only thing genuinely lost. API keys, credentials, and business logic that must stay private belong on a server. Treating a minifier as a security layer is a common route to exposed secrets.
Minifying Instead of Enabling Compression

Gzip and Brotli remove much of the same redundancy minification targets, and a server that is not compressing is leaving a far larger saving on the table than minification can offer. Check that compression is enabled before optimising further, since it is a configuration change rather than a build step and it applies to every asset you serve rather than only your JavaScript.

Quick Reference

Item

Detail

Two kinds

Whitespace-only and AST-based

Whitespace-only

Close to risk-free

AST-based

Saves more, carries more risk

ASI

Line breaks can carry meaning

return alone on a line

Returns undefined

Property names

Not mangled by default

Mangling

Breaks string-key property access

Raw percentage

Overstates the real saving

Compression

Removes similar redundancy

Priority

Enable gzip or Brotli first

Minification

Is not obfuscation or security

Public source maps

Expose your source

Minified output

Never edit it, edit the original

Frequently Asked Questions

What is a JavaScript minifier?

A JavaScript minifier reduces the size of JS files by removing characters the engine does not need, such as comments, indentation, and unnecessary whitespace. This JavaScript Minifier performs that removal in your browser, returning code that behaves identically to what you pasted while occupying fewer bytes.

Will minifying break my code?

Whitespace-only minification is close to risk-free, since it removes characters the engine ignores. Aggressive minification that renames variables and rewrites expressions carries a small but real risk, particularly around Automatic Semicolon Insertion, where removing the wrong line break can silently change what a function returns. Always test minified output before deploying it.

What is Automatic Semicolon Insertion and why does it matter?

JavaScript inserts semicolons at certain line breaks, which means a newline can carry meaning. A return followed by a value on the next line returns undefined, because ASI places a semicolon after return. A minifier that joins those lines produces different behaviour with no error, which is why line breaks cannot be treated as free whitespace.

What is the difference between minification and obfuscation?

Minification makes files smaller while keeping behaviour identical. Obfuscation deliberately makes code hard to understand, usually at a performance cost. Minified code is trivially reformatted and remains readable, so it offers no protection. Anything that must stay private belongs on a server rather than in a browser bundle.

How much smaller will my file actually be?

Smaller than raw figures suggest, because your server almost certainly compresses files before sending them, and gzip or Brotli already removes much of the redundancy minification targets. The meaningful comparison is minified-and-compressed against unminified-and-compressed, which is a narrower gap. Minification still helps, particularly with parse time, but compression is the larger lever.

Do I need source maps?

If you are debugging production errors, yes. Without a source map, an error reports a position in a single-line minified file, which is close to useless. Note that a publicly served source map exposes your original source, so proprietary projects commonly upload maps to an error tracking service instead of serving them.

Should I minify if I already use a bundler?

Minify in the bundler rather than separately. A build step runs automatically, generates source maps, keeps your source file editable, and applies consistent settings across the project. A web tool suits one-off files, snippets, and sites without a build pipeline, which is a genuine and common situation.

Is my code private when I use this tool?

Yes. This JavaScript Minifier processes everything in your browser and transmits nothing. You can verify that by opening your browser's developer tools and watching the Network tab while minifying, or by disconnecting from the internet and confirming the tool still works.

Comments

Login to leave a comment

No comments yet. Be the first to comment!