Babel Formatter
Format and beautify modern JavaScript, ES6+, JSX, and TypeScript code online. Configure indentation, semicolons, quotes, and bracket style instantly.
About
Unformatted JavaScript fails code review. Minified bundles, transpiled Babel output, and hastily written ES6+ modules produce code that is syntactically valid but structurally unreadable. This tool reconstructs indentation hierarchy by analyzing bracket depth, statement boundaries, and operator context across modern syntax including arrow functions (=>), template literals, destructuring assignments, optional chaining (?.), and nullish coalescing (??). It does not execute or transpile code. It tokenizes the input character-by-character and rebuilds whitespace according to configurable rules.
Limitation: this formatter operates on token-level heuristics, not a full AST. It handles 95% of well-formed JavaScript correctly but may misformat edge cases involving regex literals adjacent to division operators or deeply nested ternaries inside template expressions. For production pipelines, pair this with ESLint. For quick formatting of Babel output, Stack Overflow snippets, or console dumps, this tool is sufficient and requires zero installation.
Formulas
The formatter operates on a token stream, not an abstract syntax tree. Each character is classified into a token type, and indentation depth d is tracked as a simple integer counter.
Indentation Logic:
d ← d + 1 when token ∈ { {, [, ( }
d ← max(d − 1, 0) when token ∈ { }, ], ) }
Line prefix = indentChard where indentChar = spaces or tab
Where d = current indentation depth (integer ≥ 0), indentChar = repeated whitespace unit (configurable: 2 or 4 spaces, or 1 tab). The formatter scans left-to-right in O(n) time where n is the character count of the input source.
Reference Data
| Feature / Syntax | ES Version | Token Type | Formatting Rule |
|---|---|---|---|
| Arrow Functions => | ES6 (2015) | Operator | Space before & after arrow |
| Template Literals `...${}` | ES6 (2015) | String (special) | Preserve internal whitespace |
| Destructuring { a, b } | ES6 (2015) | Bracket group | Spaces inside braces |
| Spread/Rest ... | ES6 (2015) | Operator | No space after dots |
| const / let | ES6 (2015) | Keyword | Newline before declaration |
| class / extends | ES6 (2015) | Keyword | Newline before class, space after |
| import / export | ES6 (2015) | Keyword | Own line, space in braces |
| async / await | ES2017 | Keyword | Space after keyword |
| Optional Chaining ?. | ES2020 | Operator | No surrounding spaces |
| Nullish Coalescing ?? | ES2020 | Operator | Space before & after |
| Logical Assignment ??= | ES2021 | Operator | Space before & after |
| Private Fields #field | ES2022 | Identifier | Treat as normal identifier |
| Top-level await | ES2022 | Keyword | Standard await rules |
| Single-line Comment // | ES1 | Comment | Preserve, own line or inline |
| Multi-line Comment /* */ | ES1 | Comment | Preserve internal newlines |
| RegExp Literal /pattern/flags | ES3 | Regex | Preserve as-is |
| JSX Self-closing <Comp /> | JSX | JSX tag | Space before /> |
| JSX Expression {expr} | JSX | JSX expression | No padding inside braces |
| Semicolons | All | Punctuation | Configurable: insert or omit |
| Trailing Commas | ES2017+ | Punctuation | Configurable: add or remove |
| Quote Style | All | String delimiter | Configurable: single or double |