JavaScript Formatter / Minifier
Format, beautify and minify JavaScript code with Prettier-style defaults.
Reviewed by Aygul Dovletova · Last reviewed
How to Use the JavaScript Formatter
- Paste your source into the input pane. Plain JavaScript, TypeScript, ES modules, CommonJS, or a minified bundle downloaded from a CDN are all accepted.
- Choose an indent: 2 spaces (the Airbnb and Prettier default), 4 spaces (older Node conventions), or tabs.
- Press Format to break the source across lines with proper indentation around braces and brackets, or Minify to collapse the code into the smallest byte-count equivalent.
- Copy or keep iterating. The output pane retains the last result so you can toggle between Format and Minify on the same buffer without losing state.
How the Formatter Works
The implementation uses pattern-based transformations rather than a full ECMAScript parser. It tracks three contexts while scanning: inside a string literal (single, double, or template), inside a regular expression literal, and inside a block or line comment. Outside those regions, an opening brace or bracket increments an indent counter and inserts a newline; a closing brace or bracket decrements and emits a newline before it. Semicolons at statement boundaries trigger newlines so each statement ends up on its own line.
The minifier does the opposite: runs of whitespace collapse to a single space or are removed where JavaScript's automatic semicolon insertion (ASI) rules permit, and all line and block comments are stripped. Because the formatter is not a real parser, it does not rename identifiers, dead-code eliminate, or apply the dozens of size-saving transforms a production tool like Terser or esbuild would. Its job is readability round-trips, not build-time optimisation. That trade-off also means the tool tolerates TypeScript, JSX, decorators, and stage-3 proposals that would break a strict ECMAScript parser.
Typical Scenarios
- Reading a minified vendor script from a
node_modulesbundle to locate the source of a production error. - Preparing a code sample for a blog post, README, or internal wiki where indentation must match the project style guide.
- Stripping comments from a snippet before sharing it externally to remove TODOs or internal Jira IDs.
- Normalising the output of a codegen tool (Nexus, GraphQL Code Generator) whose whitespace varies between runs.
- Compressing an inline analytics snippet that will be embedded into a
<script>tag on a page with no build step. - Converting between tabs and spaces quickly when you clone a repo with a foreign editor config.
Pitfalls You Should Know About
- ASI hazards. JavaScript inserts semicolons at line breaks only in certain contexts. Minifying code that relies on implicit semicolons and starts a line with
[,(, or/can change its meaning. If your source is semicolon-less (Standard.js style), add a leading semicolon to those lines before minifying. - Template literals with nested expressions.
${'${'}...}interpolations are preserved verbatim because the tokenizer tracks template-literal depth, but deeply nested templates may receive unusual indentation inside the output. - Regex vs. division. Without a real parser,
a / banda / b /gcan be ambiguous. The tool uses simple heuristics (previous token must be a value for division) and handles the common cases, but obscure code can misread a regex. - JSX. JSX survives a round-trip but does not receive React-aware indentation (attributes on separate lines, self-closing at the end). Use Prettier for production JSX.
- Source maps. Minifying here does not produce a source map. If you need stack traces to point at original lines, minify during your build step with Terser and keep the
.mapfile.
JavaScript Language Background
JavaScript is standardised as ECMAScript, published annually by Ecma International as ECMA-262. The current edition at time of writing is ES2024. The language is whitespace-insensitive except for a handful of rules: automatic semicolon insertion at specific token boundaries, restricted productions (for example, you cannot put a newline between return and the returned expression), and the newline-sensitive behaviour of ++ and --. That is why this tool avoids rewriting anything that might affect ASI and why the minifier keeps semicolons rather than relying on implicit ones. TypeScript is a superset maintained by Microsoft; its type annotations are erased before execution and are therefore safe to preserve as plain text by a text-level formatter.
Compatibility with Prettier Defaults
The online JavaScript formatter targets Prettier's default style so that output pasted back into a Prettier-governed repo stays idempotent on the next lint pass. Those defaults are: 2-space indentation, semicolons after statements, double quotes only when the string contains a single quote (otherwise single quotes), trailing commas in multi-line arrays and objects, and an 80-character print-width hint for line breaks. Keeping those defaults matches what npx prettier --write would produce with a zero-config setup, which is the most common configuration across open-source repos. If your team overrides one of these in a .prettierrc, run Prettier locally after this tool to reconcile the two.
How It Differs from Prettier, ESLint, and Terser
Prettier builds a full AST with Babel and reprints code according to an opinionated style guide - handling line-length limits, trailing commas, and consistent quotes. Run Prettier in your repo; it always produces a better result for serious formatting. eslint --fix runs rule-driven auto-fixes (unused imports, var-to-let). Terser or esbuild --minify does semantic minification: mangles identifiers, drops dead branches, folds constants. Those require a real parser and trust in the code's correctness. This in-browser tool is for the specific case where you cannot (or do not want to) install any of those - reading a single minified snippet, prepping a one-line example for documentation, or quickly shrinking a throwaway file.
Frequently Asked Questions
Does this use Prettier or Babel under the hood?
No. It ships a small regex- and state-machine-based formatter that runs in roughly 3 KB of JavaScript. That is why the page loads instantly and works offline after the first visit. It also means the output is less polished than Prettier, which parses your code into a full AST with Babel and then prints according to dozens of tuned rules. For team-wide formatting install Prettier; for one-off inspection this tool is enough.
Can it handle TypeScript type annotations?
Yes, because the formatter operates at the token level rather than running a TypeScript parser. Type annotations, generics like <code>Array<string></code>, enum declarations, and decorator syntax all survive a round-trip. What it does not do is verify that the annotations are sound; for that, run <code>tsc --noEmit</code> locally. It also does not remove types when you minify - it treats them as plain tokens, which produces a smaller but still-typed file.
Why does my minified code occasionally break when pasted elsewhere?
JavaScript has a few places where whitespace and newlines are semantically meaningful. Specifically, lines starting with <code>(</code>, <code>[</code>, or <code>/</code> after a semicolon-less line can be reinterpreted as continuation of the previous expression. If you rely on ASI style (no explicit semicolons), add a leading <code>;</code> to those lines before minifying, or run the original file through a build tool that handles ASI correctly.
Is my code sent to any server during processing?
No. The Format and Minify buttons are wired to local JavaScript functions defined in the page bundle. There is no network request to a backend, no websocket connection, and no storage in IndexedDB. You can open the Network tab in DevTools and confirm that no outbound traffic is generated when you click either button. The input and output live in JavaScript memory and are discarded when the tab closes.
How does it compare to paste-into-editor-and-save-as-Prettier?
An in-editor Prettier run is strictly better for correctness because it parses your code, but it requires a working Node setup and your repo to have Prettier configured. This tool is worth it when you are on a machine without Node, when you want to quickly inspect a bundled vendor script, or when the source is not part of any project and you just want it readable. Consider it complementary, not a replacement.
What happens with JSX code?
JSX tokens survive the tokenizer. An element like <code><Button onClick={handle}>Go</Button></code> round-trips intact, but you will not get the React-idiomatic behaviour of breaking long attribute lists across several lines or aligning closing brackets. Prettier's JSX handling is substantially more sophisticated; use it for production React code.
Can I format an entire large file?
Yes, within the limits of your browser tab. Files under about one megabyte format in well under a second. Beyond that the single-threaded tokenizer will block the UI thread for a few seconds. If you are regularly formatting files that large, the real answer is to adopt Prettier as a pre-commit hook so nobody checks in the unformatted file in the first place.
Does it preserve comments or strip them?
Format preserves all comments (line and block) and places block comments on their own line. Minify strips comments entirely, including license banners. The tool does not currently special-case <code>/*! */</code> bang comments that licensing pipelines use to preserve copyright notices - if that matters, extract those comments before minifying, or use Terser with its <code>preserveComments</code> option.
What about modern features like optional chaining or top-level await?
They are preserved because the tokenizer does not care about semantics. Optional chaining (<code>a?.b</code>), nullish coalescing (<code>a ?? b</code>), logical assignment operators, and top-level <code>await</code> all survive. The tool will not warn you if a target runtime (older Node, older Safari) does not support them; use <code>@babel/preset-env</code> or a targeted esbuild build for that.
Why keep semicolons when minifying instead of relying on ASI?
Explicit semicolons make the minified output robust against being concatenated with other files or inlined into HTML where formatting can change. Relying on ASI saves a handful of bytes per thousand lines but introduces a real class of bugs when the output is combined with other code. Production bundlers like Terser also emit semicolons by default for the same reason.
More Developer Tools
Base64 Encoder & Decoder
Encode UTF-8 text to Base64 online or decode Base64 back to UTF-8 and plain text. Runs in your browser with no upload.
Open toolBulk URL Encode / Decode
Encode or decode many URLs at once. Paste a newline-separated list and the tool processes each line in parallel, preserving order and blank lines.
Open toolCode Screenshot
Create beautiful code snippet images with customizable themes.
Open toolColor Converter
Convert colors between HEX, RGB, HSL and CMYK formats.
Open toolCron Expression Parser
Parse cron expressions into human-readable schedules with next run times.
Open toolCSS Formatter / Minifier
Format, beautify and minify CSS code.
Open tool