JavaScript Formatter
Format JavaScript with Prettier in the browser.
Input JavaScript
190 bytes · 1 lines
Formatted JavaScript
0 bytes
What is a JavaScript Formatter?
A JavaScript formatter (or beautifier) takes minified or inconsistently-indented JavaScript and returns clean, readable source with predictable indentation, one statement per line, and consistent spacing around operators. It's the first step when reading a production bundle, debugging a third-party script, or cleaning up legacy code that was never formatted.
The major formatters in the JavaScript ecosystem are Prettier (opinionated, the modern standard in most teams) and js-beautify (configurable, decades old, still widely used especially by non-JS-first tooling). We use js-beautify here because it's a fraction of the bundle size and produces output that's indistinguishable from Prettier for most real-world code. For teams that use Prettier in their own editor, that workflow will handle formatting before the code ever gets pasted here.
Brace style is the big configuration decision. Collapse (the modern default) puts the opening brace on the same line as its statement: if (x) {. Expand puts the brace on its own line — the Allman/BSD style common in C-family codebases. End-expand keeps if/else/while on the same line as the closing brace (K&R-style). Pick the one your codebase uses and stick with it.
Indent size is 2 spaces for modern JavaScript (Airbnb, Prettier default, React convention) or 4 for older codebases and non-JS conventions. Tab-indented output is possible but strongly disfavoured in modern JavaScript ecosystems.
Limitations. A formatter only rewrites whitespace and bracket style. It doesn't reorder imports, remove dead code, or normalise variable names — those are tasks for linters (ESLint) and bundlers. If the input is syntactically invalid JavaScript, the formatter may pass it through unchanged or emit partial output; for strict validation, run the code through Node or a browser first.
Privacy: the source is formatted entirely in your browser. Useful when the script contains proprietary logic, internal API endpoints, or secrets accidentally left in code you pulled from production.
How to format JavaScript
- Paste minified JS into the input panel.
- Pick indent and brace style to match your project's house style.
- Copy or download the beautified output.
Features
- js-beautify — fast, well-tested, handles modern JS syntax.
- Indent: 2 or 4 spaces.
- Brace style: collapse, expand, end-expand.
- Preserves comments.
- Runs in your browser — source never uploads.
Frequently asked questions
- Why not Prettier?
- Prettier is excellent but 10× the bundle size of js-beautify. For a browser-based tool where the reader usually only has Prettier installed in their editor anyway, js-beautify is lighter and produces output that's indistinguishable for most code.
- What's the difference between brace styles?
- Collapse puts the opening brace on the same line (modern JS default). Expand puts the brace on its own line (C-family style). End-expand keeps else on the same line as the closing brace (K&R style).
- Does it handle JSX/TSX?
- Basic JSX passes through correctly but complex JSX expressions can get mangled. For JSX/TSX, use Prettier in your editor — it's the reference implementation.
- Will it fix my syntax errors?
- No. A formatter only rewrites whitespace. Syntactic errors pass through unchanged or produce partial output. Run the code through Node or a browser first to verify it parses.
- Can I format TypeScript?
- This tool is JavaScript-only. For TypeScript, use Prettier (first-class TS support) or tsfmt.