JavaScript Beautifier

Re-indent minified JavaScript: one statement per line, braces by depth, operators spaced, with strings, templates and regexes untouched.

Enable JavaScript to customise; default output below.

Nothing is uploaded. The formatting happens in this tab.

Indent
Live preview script.js
const panel = document.querySelector('.panel');
function toggle(open) {
	const next = open === undefined ? !panel.hidden : open;
	panel.hidden = !next;
	panel.setAttribute('aria-expanded', String(next));
	return next
}
document.addEventListener('click', (event) => {
	if(event.target.closest('.panel-toggle')) {
		toggle()
	}
});

Output is valid and updates as you type.

Re-indent minified or badly formatted JavaScript: one statement per line, braces by depth, operators spaced, and everything inside a string left alone.

How to use

  1. Paste the script, minified or otherwise.
  2. Pick the indent. Tabs, two spaces or four.
  3. Read the result. Statements are split on semicolons and braces, else, catch, finally and while stay with their closing brace, and a trailing comment stays on its line.
  4. Copy it out. This is for reading a file, not for enforcing a house style: Prettier is the tool for that.

Example

if(a){b()}else{c()}

becomes

if(a) {
	b()
} else {
	c()
}

The brace is where the structure is, so that is what the formatter uses. It does not add semicolons, add braces, or decide where a long line should wrap.

Pitfalls

  • A formatter without a parser cannot know where an expression should wrap. Long lines stay long; that is a job for Prettier, which does have a parser.
  • It does not add missing semicolons or braces. Code that relies on automatic semicolon insertion keeps relying on it, unchanged.
  • Minified code that was mangled cannot be unmangled. Variable names are gone for good; only the shape comes back.
  • A regex containing a brace or a semicolon is safe here because the tokeniser keeps it whole, which is exactly what a regular expression based formatter gets wrong.
  • Object literals are broken across lines the same way blocks are, since both use braces. A one-line object becomes three lines, which is usually what you want when reading.
  • Semicolons inside for ( ; ; ) are separators, not statement ends, and are left in place.
  • The output is not byte-identical to any particular style guide. If your project has one, run its formatter afterwards.
  • Source maps are not produced. This changes line numbers, so debug the original.

Compatibility

The tokeniser covers strings, template literals including nested ones, regular expressions, comments, numeric separators, BigInt, private class fields and multi-character operators through 2022 syntax. Everything runs in your browser with no upload.

Frequently asked questions

Is this the same as Prettier?
No. Prettier parses the code and re-prints it to a fixed style, including wrapping decisions. This re-indents what is there.
Can it recover variable names from a minified bundle?
No. Renaming is one-way; a minifier throws the names away.
Will it break my code?
It moves whitespace outside strings, comments and regexes, and nothing else.
Does it handle JSX or TypeScript?
No, both need a real parser.
Why is my long line still long?
Because deciding where to break an expression needs to understand it. That is the line between this tool and a formatter.
Weekly drops

New tools, when there are new tools

One email when something worth using ships. No schedule to fill, so no filler.

Your address goes nowhere else, and one click unsubscribes.