Skip to the content

Contact usFree tools · No signup · Files stay on your device

Developer Tools

JS Minifier: the complete guide to minifying JavaScript

JS Minifier: the complete guide to minifying JavaScript

A JavaScript minifier makes a script smaller by removing whitespace and comments, shortening local names, and rewriting code into shorter but equivalent forms. Our free JS Minifier uses Terser, the engine behind many build tools, runs in your browser, and shows the size before and after gzip. It is for developers who want to shrink a small script by hand, students checking what a minifier does, and anyone preparing a snippet for a page where there is no build step.

This guide explains each option with real before-and-after output, how syntax errors are reported, and the traps to watch for. The same approach for other file types is covered in the guides for the CSS Minifier and the HTML Minifier.

What JS Minifier does

When you paste code, the tool parses it, optimises it and prints it as compactly as possible. It can:

  • Remove comments, spaces, tabs and line breaks.
  • Shorten the names of local variables and function parameters (mangling).
  • Compress the code: remove branches that can never run, fold constants such as 1+2 into 3, inline values used only once, and drop debugger statements. The tool makes two optimisation passes.
  • Keep licence comments if you ask it to.
  • Handle modern JavaScript, including const, arrow functions, async/await, classes and template strings.

It does not bundle several files, remove console.log calls, convert modern syntax for old browsers, or generate source maps.

How to use JS Minifier, step by step

  1. Open JS Minifier.
  2. Paste your code into the box labelled "Paste JavaScript". You can also press Open file and pick a .js, .mjs or .cjs file, or press Sample.
  3. Set the four options above the panels: Mangle names, Compress (dead code, constants), ES module (import/export) and Keep /*! license */ comments.
  4. Read the result in the "Minified" panel. It updates about 0.4 seconds after you stop typing, or straight away when you change an option.
  5. Check the stats: Original, Minified, Saved, Gzip original, Gzip minified and Time.
  6. Press Copy, or Download to save script.min.js (with the site's "Tech Solution Point - " prefix).

Every option explained

All the examples below use the built-in Sample, a 503-byte script with a price formatter, an array and a loop.

Mangle names (on by default)

Shortens local names. In the sample, amount becomes e and currency becomes t, and the loop variable item becomes e. With mangling off but everything else on, the result is 330 bytes instead of 300.

Leave it on unless you depend on names at run time. Reading fn.name or relying on the name of a function parameter in a framework that inspects it are the usual cases.

Compress (dead code, constants) (on by default)

Rewrites the code, not just the spelling. In the sample, the minifier:

  • Removes if (false) { console.log("never runs"); } completely.
  • Inlines the formatter variable, so two statements become one return new Intl.NumberFormat(…).format(amount).
  • Turns the braces of the loop into a single statement.

The fully minified sample is:

/*! utils.js v2.0 | MIT License */
function formatPrice(e,t="USD"){return new Intl.NumberFormat("en-US",{style:"currency",currency:t}).format(e)}const items=[{name:"Coffee",price:3.5},{name:"Bagel",price:2.25}];let total=0;for(const e of items)total+=e.price;console.log("Total:",formatPrice(total));

That is 300 bytes, down from 503, a saving of 40.4%. With Compress off and Mangle on, the result is 347 bytes (31.0%). With both off, it is 393 bytes (21.9%): that is the saving from whitespace and comments alone.

ES module (import/export) (off by default)

Tells the tool to read your code as a module. Use it for code with import and export. The page shows a tip to switch it on if an error message mentions import or export.

It also changes how much can be renamed. In a classic script, top-level names such as formatPrice, items and total are global, so they might be used by another file or by an onclick="…" attribute. The minifier therefore leaves them alone, as you can see in the output above. In a module, top-level names are private, so Terser may rename or inline them. On the same sample, ES module changes the output to this:

/*! utils.js v2.0 | MIT License */
const e=[{name:"Coffee",price:3.5},{name:"Bagel",price:2.25}];let n=0;for(const r of e)n+=r.price;console.log("Total:",function(e,n="USD"){return new Intl.NumberFormat("en-US",{style:"currency",currency:n}).format(e)}(n));

This is smaller and still correct for a module, but it would break a script that other code calls by name. Use ES module only for code that really is a module.

Keep /*! license */ comments (on by default)

Keeps comments that start with /*!, and comments containing @license or @preserve. In the sample, the first line /*! utils.js v2.0 | MIT License */ survives and the // Format a price for display comment disappears. Untick the option and the output drops to 265 bytes in the default setup, because the licence line goes too.

Many open-source licences require their notice to stay with the code. Keep this on for any third-party code.

What it accepts and its limits

  • Input: JavaScript text from the box or from an Open file choice (.js, .mjs, .cjs).
  • Live mode: up to 300,000 characters it minifies as you type. Above that, a Minify button appears under the input with the hint "Large input (…) — click Minify to run."
  • Not supported: TypeScript, JSX and other syntax a browser would not run. Compile those first.
  • Empty input: shows "Paste JavaScript or load the sample — it's minified in your browser as you type."

Privacy

JS Minifier runs in your browser. Your code is not sent to our server. That matters for private projects and code that contains business logic. Minifying does not hide secrets, though: anyone can reverse mangled names with enough effort, and strings such as API keys stay readable in the output.

Reading syntax errors

When the code does not parse, the output box stays empty and a red note appears: "Syntax error at line 1, column 9:" followed by the reason, a snippet of the line and a caret under the problem. The line is counted from 1 and so is the column. Three real examples:

Your code What the note says
let x = ; Syntax error at line 1, column 9: Unexpected token: punc (;)
const a = 1; then const a = 2; on the next line Syntax error at line 2, column 7: "a" is redeclared
console.log(1) then console.log(2 on the next line Syntax error at line 2, column 14: Unexpected token eof «undefined», expected punc «,»

The third message looks odd. "eof" means the end of the file: the code stopped before a closing bracket. When you see it, check for a missing ) or } somewhere above the position.

Tips to work faster

  • Test the minified code. Run it, or paste it into a page, before you publish it.
  • Compare Mangle names on and off when something misbehaves; it narrows the cause quickly.
  • Keep console.log lines out of the production copy yourself, because Compress will not delete them.
  • For an inline script inside a page, you can also tick Minify inline CSS & JS in the HTML Minifier.
  • Watch the gzip numbers. Gzip already squeezes whitespace, so the gap between "Gzip original" and "Gzip minified" is smaller than the raw saving, and it is the figure that matches what visitors download.

Common problems and how to fix them

"Syntax error … Unexpected token"

The code itself is invalid, or it uses syntax the engine does not parse, such as TypeScript types or JSX. Fix the line shown by the caret. If the message mentions import or export, tick ES module.

The minified script stopped working

Suspect names first. Untick Mangle names, then Compress, one at a time. Code that reads fn.name, or that is called by name from another file, is the usual culprit. For the second case, make sure ES module is off.

My licence comment vanished

It must start with /*! or contain @license or @preserve, and Keep /*! license */ comments must be ticked.

Nothing happens with a large file

Above 300,000 characters the tool waits for you. Press the Minify button.

The result is only a little smaller

Already compact code, or code that is mostly strings and data, has little to shrink. The saving comes from names and whitespace, so a script full of long text will barely change.

Quick checklist

  • Keep Mangle names and Compress on for normal code.
  • Tick ES module only for code with import or export.
  • Start licence notices with /*!.
  • Read the line and column when you get a syntax error.
  • Test the minified file before you publish it.

Frequently asked questions

Will minified JavaScript behave exactly like the original?
For valid code, yes. Terser rewrites the code into a shorter form with the same meaning. The exceptions are code that depends on names at run time, such as reading a function's name, or code that relies on a top-level variable being visible from another script.
Why are my top-level function names not shortened?
With ES module switched off, the code is treated as a classic script where top-level names are global and other files or inline handlers may call them, so they stay. Tick ES module to let the minifier rename top-level names too.
What is the difference between Mangle names and Compress?
Mangle names shortens the names of local variables and parameters, such as amount becoming e. Compress rewrites the code itself: it removes unreachable branches, folds constants, inlines single-use values and drops debugger statements. You can switch each one on or off.
Does the minifier remove my console.log lines?
No. Compress removes dead code and debugger statements, but a console.log that can run stays in the output. Delete the logs yourself if you do not want them in production.
Which comments are kept with Keep /*! license */ comments?
Comments that start with /*!, plus comments containing @license or @preserve. Every other comment, including // line comments, is removed.
Found this useful? Share it

Tools in this guide

Main tool

JS Minifier

Shrink JavaScript for production with Terser — the same minifier used by webpack, Vite and Next.js.

Open tool

CSS Minifier

Compress your stylesheets for faster pages.

Open tool

HTML Minifier

Make HTML pages smaller without breaking them.

Open tool

Back to all guides & blog