Home › Developer Tools › JavaScript Minifier

JavaScript Minifier

Paste or drop your JavaScript and get a smaller file in one click. Terser compresses expressions and shortens variable names; a built-in minifier removes comments and whitespace safely if Terser can't load. Your code never leaves the browser.

Last updated:

Original–
Minified–
Saved–
Advertisement

How to use the JavaScript Minifier

  1. Paste your JavaScript into the left box, drop a .js file onto it, or press Open .js file.
  2. Choose options: Compress and Mangle give the smallest output; tick Mangle top-level names only for self-contained scripts.
  3. Press Minify JavaScript. The output updates automatically as you type or change options.
  4. Check the result: the engine badge shows whether Terser or the built-in minifier ran, and the cards show bytes saved and gzip size.
  5. Copy or download the minified code as a .min.js file.

What minification changes, with an example

Take this function (152 bytes):

// Returns the total price including tax
function totalPrice(items, taxRate) {
  const subtotal = items.reduce((sum, item) => sum + item.price, 0);
  return subtotal * (1 + taxRate);
}
StepOutputSize
Remove comments and whitespacefunction totalPrice(items,taxRate){const subtotal=items.reduce((sum,item)=>sum+item.price,0);return subtotal*(1+taxRate)}113 bytes
+ Mangle local namesfunction totalPrice(e,t){const r=e.reduce((e,t)=>e+t.price,0);return r*(1+t)}78 bytes
+ Compress (inline the temporary variable)function totalPrice(e,t){return e.reduce((e,t)=>e+t.price,0)*(1+t)}69 bytes

The function name totalPrice is kept because other code may call it. Ticking Mangle top-level names would shorten it too.

Terser options explained

OptionWhat it doesRisk
CompressDead-code removal, constant folding, joining declarations, shorter conditionals (two passes)Very low for standard code
Mangle variable namesRenames local variables and parametersLow; code that relies on Function.prototype.name of local functions may change
Mangle top-level namesAlso renames and drops unused top-level functions and variablesBreaks globals used by other scripts or inline HTML handlers
Drop console.* callsRemoves console.log, console.warn and similarArguments with side effects are removed too
Keep /*! license */ commentsPreserves comments starting with /*! or containing @license / @preserveNone; required by many open-source licences

Why automatic semicolon insertion matters

JavaScript inserts semicolons at some line breaks, so blindly deleting new lines can change what code does. For example return followed by a value on the next line returns undefined, and a followed by ++b on the next line increments b, not a. The built-in fallback keeps a line break wherever removing it could change the meaning, and removes it where the previous token is an operator, comma, semicolon or opening bracket. It also knows when a / starts a regular expression rather than a division, so patterns like /[/]+/g and strings containing // are never mistaken for comments.

Frequently asked questions

What does a JavaScript minifier do?

It rewrites your code into the smallest equivalent form: comments and whitespace are removed, local variable names are shortened (mangling), and expressions are simplified (compression). The code behaves the same but downloads and parses faster.

Is minified JavaScript safe to use in production?

Yes, that is what minification is for. Terser is the minifier used by webpack, Vite and Rollup. Keep the original source for editing and debugging, and test the minified build before you deploy it.

What is the difference between compress and mangle?

Compress rewrites code: it removes unreachable code, joins variable declarations, folds constants and shortens if statements. Mangle renames local variables and function parameters to one or two letters. Using both gives the smallest file.

When should I enable Mangle top-level names?

Only when the script is self-contained, such as a bundle or an ES module. Top-level mangling renames global functions and variables, which breaks code that other scripts or HTML onclick attributes call by name.

What happens if Terser cannot load?

Terser is loaded from a CDN the first time you minify. If that fails, for example offline or behind a firewall, the built-in minifier removes comments and unnecessary whitespace and line breaks while respecting strings, template literals, regular expressions and automatic semicolon insertion. The page tells you which engine ran.

Why do I get a syntax error?

The minifier must parse your code first, so invalid JavaScript is rejected with the line and column of the problem. Common causes are a missing bracket, a stray character, or TypeScript or JSX syntax, which must be compiled to plain JavaScript before minifying.

Is my code uploaded?

No. Minification runs in your browser. Only the Terser library itself is downloaded from a public CDN; your source code is never sent anywhere.

Are all the tools on FreeToolCabin tested for quality and accuracy?

Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.

Related tools

JavaScript BeautifierFormat and beautify messy or minified JavaScript, or minify it.JSON FormatterBeautify, minify and validate JSON, with a collapsible tree view.HTML FormatterBeautify and indent HTML code with a live preview.CSS FormatterBeautify CSS with indent controls and optional property sorting.Browser BenchmarkTest your browser's speed: JavaScript, DOM, Canvas, WebGL, JSON and regex scores in one run.Core Web Vitals CheckerCheck real-user LCP, INP and CLS for any URL or site and see if it passes Core Web Vitals.JSON MinifierMinify and validate JSON, sort keys and see exactly how many bytes you save.SRI Hash GeneratorCreate Subresource Integrity hashes (sha256/384/512) and ready <script> or <link> tags.SQL Formatter & BeautifierFormat, beautify or minify SQL queries for MySQL, PostgreSQL and more.XML FormatterBeautify, validate and minify XML with a collapsible tree view.ZIP File CreatorCompress files and folders into a ZIP.YAML Validator & FormatterValidate, format and minify YAML, and convert YAML to JSON or back.HTML MinifierMinify HTML: strip comments and whitespace safely and see how many bytes you save.CSS MinifierMinify CSS with csso or a built-in engine and see the size saved instantly.Code Diff CheckerCompare two code files side by side or unified, with syntax colors and changes highlighted.IPv6 Expander & CompressorExpand IPv6 addresses to full form or compress them per RFC 5952, one or many at once.GraphQL FormatterPretty-print and indent GraphQL queries, mutations and schemas, or minify them, instantly.CSS ValidatorValidate CSS: syntax errors, unknown properties and invalid values by line, plus auto-fix.Gzip Compress / DecompressGzip or deflate text and files, get Base64 output, and decompress Base64 or .gz files.IBAN ValidatorCheck an IBAN's mod-97 checksum, country and length, and see its bank code and account parts.JavaScript Syntax CheckerCheck JavaScript for syntax errors with line and column, for any ES version, script or module.JSON Schema ValidatorValidate JSON against a JSON Schema (draft-07, 2019-09, 2020-12) with clickable error paths.Markdown FormatterBeautify Markdown with Prettier: list markers, prose wrap, aligned tables and live preview.PHP FormatterBeautify PHP code with Prettier PHP: PSR-12 style, indent and brace options, syntax errors.TypeScript FormatterFormat TypeScript and TSX code with Prettier: width, tabs, semicolons and quotes options.XML MinifierMinify and validate XML: strip whitespace and comments, keep CDATA, see the size savings.C# FormatterBeautify C# code online: Allman or K&R braces, indent size and line width, clang-format option.HTML ValidatorCheck HTML for errors and warnings with line numbers, locally or with the W3C Nu checker.Java FormatterBeautify Java code online: indent size, brace style and line width, with clang-format option.JavaScript ObfuscatorObfuscate JavaScript with string encoding, renaming and control-flow flattening presets.Phone Number FormatterFormat and validate lists of phone numbers to E.164, international or national style.Python FormatterFormat Python code in Black/Ruff style: line length, quote style, indent, copy or download.Responsive Design CheckerPreview any website at phone, tablet, laptop and desktop sizes side by side, with rotate and zoom.Browser Compatibility CheckerSearch CSS, JavaScript and HTML features and see which browser versions support them.Heading Structure CheckerPaste HTML to see its H1-H6 outline and catch missing H1s, multiple H1s and skipped levels.Website Hosting CheckerFind out who hosts a website: hosting provider, IP addresses, ASN, network owner and location.Routing Number ValidatorCheck a US ABA routing number's checksum and format and see its Federal Reserve district.Spam Word CheckerScan an email subject and body for spam trigger words, excess caps, exclamation marks and risky links.Strftime Format BuilderBuild Python and C strftime format strings by clicking tokens, with a live preview and reference.Check Digit CalculatorCalculate or validate check digits for UPC, EAN, GTIN, SSCC, ISBN, ISSN and mod 11 codes.Luhn Algorithm CheckerValidate numbers with the Luhn mod 10 check or calculate the missing check digit, step by step.XSD ValidatorValidate XML against an XSD schema in your browser and see each error with its line number.
See all 41 Code Formatters & Validators →