Code Formatter
Paste any curly-brace code (JS, PHP, Java, C, C++, CSS, JSON...) β blank lines are stripped and lines are re-indented by brace depth.
How Code Formatter Works
This tool re-indents any curly-brace code -- JavaScript, PHP, Java, C, C++, CSS, JSON, and similar languages -- by tracking how deeply each line sits inside nested { } blocks, strips out blank lines, and checks whether every opening brace has a matching closing one.
Formula & Method
The formatter is intentionally not a full per-language parser -- it works purely off curly braces, which are the strongest language-agnostic indentation signal shared by C-like, JSON, and CSS-like code. It first removes every blank line, then walks the remaining lines while tracking a running nesting depth: a line that starts with one or more closing braces is dedented before those closes are applied, and each line is indented by depth × indent size spaces (indent size is adjustable, default 4). Depth increases by the number of { on a line and decreases by the number of }, clamped so it never goes below zero for display purposes. Separately, brace balance is checked over the raw, unclamped count across the whole text: if the running count ever dips below zero, there's at least one stray closing brace with no matching opener; if it doesn't return to exactly zero by the end, there's at least one opening brace with no matching closer.
Worked Example
Given this snippet with extra blank lines and no indentation -- function foo() {, a blank line, if (x) {, bar();, }, a blank line, } -- with indent size set to 2, the formatter removes the 3 blank lines, reports a max nesting depth of 2, confirms the braces are balanced, and outputs the code re-indented as function foo() { / if (x) { / bar(); / } / }.
Frequently Asked Questions
- Does this formatter understand PHP, Python, or other specific language syntax?
- No -- it only tracks curly-brace nesting, so it works the same way across any curly-brace language (JS, PHP, Java, C, C++, CSS, JSON). It doesn't parse language-specific constructs like Python's indentation-based blocks, string contents, or comments, so braces that appear inside a string or comment are still counted as real braces.
- What does "Unbalanced" mean in the result?
- It means the number of opening
{and closing}characters in your code don't match up -- either an extra closing brace with nothing to close, or an opening brace that's never closed. The warning message tells you which of these occurred and roughly how many. - Why were some of my blank lines removed?
- The formatter strips every blank (whitespace-only) line before re-indenting, as part of tidying up the code. If you need to preserve deliberate spacing between sections, add a comment line instead of an empty line.
- Can I change how many spaces are used per indent level?
- Yes -- the indent size slider controls how many spaces represent one nesting level, from 1 to 8 spaces, and the formatted output updates immediately when you change it.