JavaScript Minifier

Minify your JavaScript code by removing whitespace, comments, and unnecessary characters. Compare file sizes instantly.

What Is JavaScript Minification?

JavaScript minification is the process of removing all unnecessary characters from JavaScript source code without altering its functionality. These unnecessary characters include whitespace, line breaks, comments, and sometimes redundant semicolons or brackets. The result is a much smaller file that the browser can download and parse faster, directly improving page load performance.

When developers write JavaScript, they use indentation, descriptive variable names, blank lines, and inline comments to make the code readable and maintainable. This formatting is essential during development but serves no purpose at runtime. The JavaScript engine in a browser ignores whitespace and comments entirely. Minification exploits this by stripping away everything the engine does not need, producing a compact version of the same program.

A typical JavaScript file can shrink by 20 to 60 percent after minification, depending on how heavily commented and formatted the original source is. For large applications with hundreds of kilobytes of JavaScript, this translates into noticeably faster page loads, especially on slower mobile connections. Every byte saved is a byte that does not need to travel over the network, decompress on the client, or occupy memory in the browser.

How JavaScript Minifiers Work

At the most basic level, a JavaScript minifier reads the source code character by character and decides what to keep and what to discard. The process involves several distinct steps that must be performed carefully to avoid breaking the code.

Comment Removal

JavaScript supports two types of comments: single-line comments that begin with // and continue to the end of the line, and multi-line comments that start with /* and end with */. A minifier must remove both types, but it cannot blindly search for these character sequences. The characters // and /* can also appear inside string literals, template literals, and regular expression literals. A correct minifier tracks whether it is currently inside a string or regex and only treats comment markers outside of those contexts as actual comments.

Whitespace Collapsing

After comments are removed, the minifier collapses whitespace. Multiple consecutive spaces, tabs, and newlines are replaced with a single space or removed entirely. Whitespace around operators like =, +, , and ; is generally safe to remove because the JavaScript parser does not require it in most contexts. However, whitespace between identifiers and keywords must be preserved. For example, return value cannot become returnvalue because that changes the meaning of the code. The minifier must understand where a space is syntactically meaningful and where it is purely cosmetic.

String Literal Preservation

The contents of string literals must never be modified. A string enclosed in single quotes, double quotes, or backticks (template literals) may contain any characters, including sequences that look like comments or extra whitespace. A minifier that strips // from inside a string would corrupt the data. Proper minifiers use a state machine to track whether they are inside a string, and they pass string contents through unmodified. Escaped characters within strings, such as \" or \\, must also be handled correctly so the minifier does not mistakenly interpret an escaped quote as the end of the string.

Line Break Handling

JavaScript has a feature called Automatic Semicolon Insertion (ASI). In certain situations the parser inserts a semicolon at the end of a line if one is missing. This means that removing a line break can change the meaning of code. For example, a return statement followed by a newline and then a value will return undefined instead of the value, because ASI inserts a semicolon after return. Advanced minifiers are aware of ASI rules and insert semicolons explicitly where needed before removing line breaks. Simpler minifiers may leave certain line breaks in place to avoid this class of bugs.

Benefits of Minifying JavaScript

The primary benefit of minification is reduced file size, but the downstream effects touch many aspects of web performance and user experience.

Faster Page Loads

Smaller files download faster. On a 3G mobile connection, saving 50 KB of JavaScript can shave hundreds of milliseconds off the load time. Because JavaScript is render-blocking by default, every millisecond spent downloading and parsing scripts is a millisecond the user spends staring at a blank or incomplete page. Minification is one of the simplest and most effective optimizations you can apply.

Lower Bandwidth Costs

If your site serves millions of requests per month, the cumulative bandwidth savings from minification add up significantly. Hosting providers and CDNs charge by the gigabyte. Reducing every JavaScript response by 30 to 50 percent directly reduces your infrastructure costs without requiring any changes to your application logic.

Improved Core Web Vitals

Google's Core Web Vitals, particularly Largest Contentful Paint (LCP) and First Input Delay (FID), are influenced by how quickly the browser can download, parse, and execute JavaScript. Minified JavaScript contributes to better scores on these metrics, which in turn can improve search engine rankings. The performance gains are modest compared to code splitting or lazy loading, but minification is a zero-effort win that every production site should use.

Reduced Parse Time

Browsers must parse every byte of JavaScript before executing it. Fewer bytes mean less work for the parser. On low-powered devices such as budget smartphones, the parsing phase can account for a significant portion of the total script execution time. Minification reduces this overhead directly.

Minification vs Uglification vs Compression

These three terms are frequently confused, but they refer to distinct processes that can be used together for maximum size reduction.

Minification

Minification removes formatting characters (whitespace, line breaks, comments) without renaming anything. The output is still valid JavaScript with the same variable names, function names, and structure. It is the safest transformation because it only discards characters that the runtime ignores. This tool performs minification.

Uglification (Mangling)

Uglification goes a step further by renaming local variables and function parameters to shorter names. A variable called userAccountBalance might become a. This produces smaller files than minification alone but makes the output nearly impossible to read. Tools like Terser and UglifyJS perform both minification and uglification. Mangling is generally safe for local variables but can cause issues with code that relies on Function.name or dynamic property access patterns.

Compression (Gzip / Brotli)

Compression is a server-side process that encodes the response body using algorithms like Gzip or Brotli. It works on any text content, not just JavaScript, and achieves typical compression ratios of 60 to 80 percent. Compression and minification are complementary: minified code compresses slightly less efficiently than formatted code (because there is less redundancy), but the total size after both steps is always smaller than either alone. Most production deployments use minification at build time and compression at serve time for the best results.

Popular Build Tool Integrations

While online tools like this one are convenient for quick one-off tasks, production applications typically integrate minification into their build pipeline so it happens automatically on every deploy.

Webpack

Webpack uses TerserPlugin by default in production mode. When you run webpack --mode production, all JavaScript bundles are automatically minified and mangled. You can customize the Terser configuration to preserve certain comments (such as license headers) or disable specific transformations. Webpack 5 also supports parallel minification across multiple CPU cores for faster build times on large projects.

Vite and Rollup

Vite uses esbuild for development builds and Rollup with Terser for production builds. The vite build command produces minified output by default. Because esbuild is written in Go, it is extremely fast, handling minification in milliseconds even for large bundles. Rollup's tree-shaking also removes unused code before minification, further reducing output size.

esbuild

esbuild is a standalone bundler and minifier that is orders of magnitude faster than JavaScript-based tools. Running esbuild --minify performs whitespace removal, comment stripping, and identifier mangling in a single pass. It has become the preferred choice for projects that prioritize build speed, and many frameworks use it internally.

SWC

SWC is a Rust-based compiler that also includes a minifier. Next.js uses SWC by default for both compilation and minification. Like esbuild, SWC achieves its speed advantage by being written in a compiled language rather than JavaScript. Its minifier supports the same configuration options as Terser, making migration straightforward.

Terser (Standalone)

Terser is the successor to UglifyJS and the most widely used JavaScript minifier in the ecosystem. It can be invoked from the command line with npx terser input.js -o output.min.js -c -m. The -c flag enables compression (dead code elimination, constant folding), and -m enables mangling. Terser understands modern ES2015+ syntax, including arrow functions, destructuring, template literals, and async/await.

Closure Compiler

Google's Closure Compiler is the most aggressive JavaScript optimizer available. In its advanced mode, it performs whole-program analysis, renames both local and global variables, inlines functions, and removes unreachable code. The output can be dramatically smaller than what other tools produce, but advanced mode requires code to be annotated with JSDoc type information and follow specific coding conventions. Simple mode behaves more like Terser and is safer for general use.

About This Tool

This JavaScript minifier runs entirely in your browser. Your code is never sent to a server, making it safe to use with proprietary or sensitive source code. The minifier processes the input character by character, correctly handling string literals (single-quoted, double-quoted, and template literals) so that comment-like sequences inside strings are preserved. It removes single-line and multi-line comments, collapses redundant whitespace, and strips blank lines to produce compact output.

After minification, the stats bar shows the original size in bytes, the minified size in bytes, and the percentage reduction. This gives you an immediate sense of how much bandwidth you will save by serving the minified version. For production use, we recommend combining this with Gzip or Brotli compression on your server for maximum savings.

This tool performs whitespace and comment removal only. It does not rename variables (mangle), inline constants, or perform dead-code elimination. For those advanced optimizations, use a build tool like Terser, esbuild, or SWC as part of your automated build pipeline. However, for quick prototyping, reviewing compression potential, or minifying small scripts and bookmarklets, this browser-based tool is fast and convenient.

Paste your JavaScript into the input area, click Minify, and copy the result. You can also use the Clear button to reset both fields and start over. The tool accepts any valid JavaScript, including ES2015+ syntax with arrow functions, template literals, destructuring, and async/await.