Skip to main content

JavaScript Minifier & Formatter

Optimize your JS code instantly. Minify JavaScript to reduce file size for production, or beautify minified code to make it human-readable again.

Input JavaScript
Output Result
Ready
Original 0 B
Output 0 B
Compression 0%

Advertisement

JavaScript Minification, Token Protection, and Code Beautification

Maybe you just copied a compressed tracking snippet from a vendor dashboard and need to verify what it actually sends before placing it on your site. Or you wrote a standalone helper script for an HTML widget and want to shrink out comments and indentation without installing Node or configuring Webpack. Both jobs sit on opposite sides of the same parser problem: JavaScript engines care about syntax tokens, while humans need indentation, whitespace, and notes to understand control flow.

Unlike images or decorative fonts, unoptimized JavaScript carries a double performance penalty. First, bytes travel across the network, delaying script fetch and HTML parsing if the script tag lacks defer or async. Second, the JavaScript engine still has to scan every comment, space, and blank line on the main thread before compiling bytecode. Stripping formatting noise lowers transfer payload immediately and pairs cleanly with CSS minification for static sites without a complex bundler.

How This Token Preserving Minifier Protects Strings and Regex

Naive find and replace scripts break JavaScript almost instantly because comments, strings, and regular expressions reuse the exact same slash and quote characters. Deleting everything after // in naive regex wipes out https://example.com inside a quoted URL, and stripping spaces inside backtick templates breaks formatted messages.

Inside this tool, clicking Minify walks the source character by character before collapsing any whitespace. Quoted strings ("...", '...'), backtick template literals (`...`), and regex literals (/pattern/gi) are captured verbatim into a temporary token table while single line // and block /* */ comments are removed outside those strings. It even checks the previous operator or keyword (such as return, typeof, =, or () to tell whether a forward slash opens a regular expression or performs division, and keeps spaces between adjacent plus or minus operators so a + +b never collapses into an invalid a++b increment.

Structural Minification Versus Build Step Compilers

Choosing the right minifier depends on whether you are shrinking a quick snippet in the browser or bundling a full application in CI:

Transformation This Browser Tool AST Bundlers (Terser / esbuild)
Strip comments and extra spaces Yes Yes
Keep strings, templates, and regex intact Yes Yes
Preserve original function and variable names Yes (easier stack traces) Usually mangled to a, b
Tree shaking and dead code removal No Yes, across module imports
Semicolon insertion when line breaks collapse Assumes semicolons are present Rewrites AST boundaries automatically

Watch Out for Automatic Semicolon Insertion (ASI)

Because structural minification collapses line breaks into single spaces, semicolon placement matters before you compress raw code. In JavaScript written without explicit semicolons, line endings act as invisible statement terminators. Once Minify joins adjacent lines onto one horizontal row, a pattern like:

const count = 4
const label = "items"

merges into const count=4 const label="items", throwing a syntax error in the browser console. An even subtler bug hits return followed by a newline before an object literal: JavaScript inserts an implicit semicolon right after return, returning undefined instead of the object even before minification runs. Keeping explicit semicolons cleanly (or formatting with Beautify first to spot bare line endings) prevents ASI bugs before you deploy compressed snippets.

Advertisement

Reversing Minified Scripts with Beautify

Clicking Beautify runs the open source js-beautify formatter locally inside your browser with four space indentation, empty parenthesis spacing, and up to two preserved blank lines. It transforms a solid single line bundle into structured blocks with live syntax highlighting so you can audit third party code, inspect minified event handlers, or extract API endpoints without squinting.

Two things cannot return from a minified file, regardless of which formatter you use: deleted comments and mangled local variable names. If a production bundle turned checkoutSessionToken into e via Terser, beautifying restores clean block nesting and readable calls, while local parameter names stay e unless you load the original source map in browser DevTools. Notice also that syntax highlighting renders automatically after clicking Beautify, whereas clicking Minify intentionally turns the highlight overlay off so compressing multi thousand character single line scripts stays fast and lag free.

Common Edge Cases When Pasting Snippets

Before dropping minified output straight into a live HTML page or bundle, watch for three practical quirks that trip up pasted scripts:

  • Unclosed block comments: Copying an incomplete snippet that opens /* without a matching */ causes comment stripping to consume everything after it. If output shrinks suspiciously to a tiny stub, run Beautify first or check trailing characters in the input box.
  • Closing script tags inside strings: If your JavaScript holds a string like "</script>" and gets pasted inline inside an HTML <script> element, the browser's HTML parser ends the script block early even though JavaScript syntax is valid. Escape the slash as "<\/script>" when embedding inline.
  • Open source license banners: Structural minification strips every // and /* */ comment without making an exception for legal notices. If a distributed MIT or Apache library requires retaining its copyright header, paste that short block back at the top of script.min.js before publishing.
  • Gzip and Brotli interplay: Web servers compress responses on the wire, which shrinks repeated spaces heavily. Even so, stripping long explanatory comments and JSDoc blocks cuts both compressed download size and the uncompressed byte count the browser engine must tokenize on startup.

File Export, Swap, and Compression Metrics

Notice how the toolbar actions adapt as you work. You can drag and drop a file or click Upload .JS to inspect existing scripts, click the center Swap arrow button to send formatted code back to the input pane for another edit pass, and watch the status footer report exact byte sizes alongside the Compression percentage. Notice that compression shows a negative reduction only after beautifying compact code, because adding four space indentation expands raw bytes for readability. When you click Save JS, the download automatically switches filenames based on your last run: saving script.min.js after minifying or script.formatted.js after beautifying. If your script wraps JSON configuration payloads inside the code, check those data blocks separately with the JSON Formatter & Validator before bundling.