Code Formatter: Format JS, Python, HTML & More
Format and indent code in 12 languages, including JavaScript, Python, HTML, and CSS. Runs in the browser only, with no signup and no server upload. Free to use.
Code Formatter
Format your code with a single click. Select a language, paste your code, and get it properly formatted.
How to Use:
- Select your programming language from the dropdown.
- Paste your unformatted code in the input area.
- Click the 'Format Code' button.
- Copy the formatted result from the output area.
Documentation
What is a code formatter?
A code formatter is a tool that rewrites source code with consistent indentation, line breaks, and spacing, without changing what the code does. This code formatter runs in a web browser. It supports 12 programming languages, including JavaScript, Python, HTML, and CSS.
How this code formatter works
The tool uses a different set of rules for each language. None of them build a full parse tree of the code, except JSON.
- JSON is parsed with the browser's built-in JSON parser and then printed back out with two-space indentation. If the JSON is invalid, the tool reports an error and shows no output.
- JavaScript, TypeScript, Java, C, C++, and C# share one formatter. It scans the code character by character, tracking curly braces, parentheses, and semicolons. Every
{starts a new indented block, and every}or;ends a line. Comments and quoted strings are copied through untouched. This formatter does not add spaces around operators or after commas that the original code was missing — it only rebuilds line breaks and indentation. - Python reads each line's existing indentation and rebuilds it using four spaces per level, following the same nesting rules the Python language itself uses. It does not split long lines, add blank lines, or add spacing around operators; it only normalizes how deep each line is indented.
- CSS removes extra line breaks and blank space, then adds a line break after each
{,;, and before each}. - HTML and XML add a line break and two spaces of indentation for each level of nested tags, recognizing standard self-closing tags such as
<br>,<img>, and<input>. - SQL converts a fixed set of keywords (such as
SELECT,FROM,WHERE,JOIN, andORDER BY) to uppercase and starts a new line before each major clause.
Because only the JSON path actually parses the input, none of the other formatters detect syntax errors such as a missing bracket or an unclosed string. They reformat whatever text they are given, even if it would not run.
Supported languages
| Language | Typical file extension | Indent width |
|---|---|---|
| JavaScript | .js | 2 spaces |
| TypeScript | .ts | 2 spaces |
| HTML | .html | 2 spaces |
| CSS | .css | 2 spaces |
| Python | .py | 4 spaces |
| Java | .java | 4 spaces |
| C | .c | 4 spaces |
| C++ | .cpp | 4 spaces |
| C# | .cs | 4 spaces |
| SQL | .sql | line breaks by clause |
| JSON | .json | 2 spaces |
| XML | .xml | 2 spaces |
How to format code
- Select a programming language from the dropdown menu.
- Paste the unformatted code into the input box.
- Select "Format Code."
- Copy the formatted result from the output box.
Formatting happens entirely inside the browser. The code is never sent to a server.
JavaScript formatting example
Before formatting:
1function add(a, b) {
2 if (a > 0) { return a + b; }
3 return 0;
4}
5After formatting:
1function add(a, b) {
2 if (a > 0) {
3 return a + b;
4 }
5 return 0;
6}
7The formatter moved the code inside { } onto its own indented line and normalized the indentation to two spaces per level. It left the spacing around a > 0 and a + b exactly as it found it, because that spacing was already there.
Python formatting example
Before formatting:
1def calculate_average(numbers):
2 if len(numbers) == 0:
3 return 0
4 total = sum(numbers)
5 return total / len(numbers)
6After formatting:
1def calculate_average(numbers):
2 if len(numbers) == 0:
3 return 0
4 total = sum(numbers)
5 return total / len(numbers)
6The original code used two spaces per indent level. The formatter rebuilt each line's indentation using four spaces per level, matching the style described in PEP 8, Python's official style guide. It kept every line in the same order and did not touch spacing inside the lines.
Why formatting code matters
Inconsistent indentation makes it harder to see where a function, loop, or condition ends. Code that several people have edited often ends up with a mix of tab and space indentation, or lines that don't line up with the block they belong to. Running it through a formatter gives every file the same visual structure, which makes it faster to scan and easier to spot a misplaced bracket.
Limitations
- Input size: The tool accepts up to 50,000 characters of code. Longer input is rejected with an error message before formatting starts.
- No syntax checking for most languages: Only JSON input is parsed and validated. Code in the other 11 languages is reformatted as-is, even if it has a missing bracket or an unclosed string. The output in that case may be misindented rather than flagged as an error.
- No operator spacing changes: The JavaScript, TypeScript, and C-family formatters only rearrange line breaks and indentation. They do not insert a space around
+,=,!==, or after a comma if one wasn't already there. - PHP is not supported: Selecting an unsupported language returns the code unchanged.
Frequently asked questions
Does this tool check my code for syntax errors? Only for JSON. JSON input is parsed, so invalid JSON produces an error message. Code in the other supported languages is reformatted without being checked for valid syntax.
Does it send my code to a server? No. Formatting runs entirely in the browser using JavaScript. The code is not uploaded or stored anywhere.
What is the maximum amount of code it can format? 50,000 characters. Pasting more than that produces an error before formatting starts.
Will it add missing spaces around my operators or after commas? No, for JavaScript, TypeScript, Java, C, C++, and C#. Those formatters only rebuild indentation and line breaks around braces and semicolons; they copy the rest of each line as written.
What indentation does it use? Two spaces for JavaScript, TypeScript, HTML, CSS, JSON, and XML. Four spaces for Python, Java, C, C++, and C#.
Why does my code look almost the same after formatting? If the input already used consistent spacing and one style of brackets, the formatter mainly rebuilds indentation, so the change may be small. It is most useful on code with mixed indentation or code that has been minified onto very few lines.