Quick access

Saved in this browser. Sign in to keep it on all your devices.

Code Formatter

Format and beautify code in multiple languages.

Input
Output
Lines: Size:
Version 1.1 Updated What's new

Release notes

  1. v1.1 Latest
    • Added an in-depth guide below the tool, written from what the tool actually does.
    • Corrected guide text that overstated what the formatter does.

About the Code Formatter

The code formatter breaks a wall of minified code onto separate lines so you can read it. JSON is fully parsed and indented. HTML, CSS and JavaScript get quick line breaks from pattern matching, which is good for inspecting code but is not a replacement for a formatter in your editor.

How to use it

  1. 1 Choose the language.
  2. 2 Paste your code.
  3. 3 Format it and copy the result.

Frequently asked questions

Does formatting change what my code does?

JSON keeps its meaning, except that very large integers can lose precision. The other modes work by pattern matching and can add a line break inside a quoted string, which changes the string or breaks the code. Treat their output as something to read, not something to ship.

Can it un-minify JavaScript?

It adds line breaks so you can follow the statements, but it does not indent nested blocks and it cannot bring back variable names that were shortened during minification. Only a source map can restore those.

Is my code uploaded?

No. Formatting runs in your browser.

Why did my JSON come back unchanged?

The JSON was invalid, so the parser rejected it and the tool returned your input as it was, without an error message. Trailing commas, single quotes and comments are the usual causes. The JSON Formatter shows the exact line and column of the problem.

Can it format TypeScript, JSX, PHP or Python?

No. The five modes are JavaScript, JSON, HTML, CSS and SQL. TypeScript and JSX would go through the JavaScript patterns and come out with the same problems, and Python depends on its indentation, so adding line breaks to it would break it.

What do the Lines and Size figures show?

They describe the output box: how many lines it has and how many characters it holds, counting spaces and line breaks. Compare the size before and after Minify to see how much of a file was whitespace.

What this formatter actually does

Minified code arrives as one enormous line, which is fast for browsers to download and close to impossible for people to read. This tool gets you from that line to something you can scan. It has two buttons. Format adds line breaks according to the language you pick, and Minify does the reverse by squeezing the code back onto a single line.

The important thing to know is that only one of the five modes understands the code it is given. JSON mode runs your text through a real parser. The JavaScript, HTML, CSS and SQL modes search for characters such as semicolons and braces and put line breaks around them, without knowing whether those characters are code or part of a quoted string. That makes them quick and forgiving of broken input, but it limits what you should do with the result.

How each language is handled

  • JSON: parsed with the browser's built-in JSON parser, then written back out with two spaces per level. This is the only mode that produces true nested indentation, and it follows the JSON standard (RFC 8259) strictly.
  • CSS: a line break after each opening brace and semicolon, each property indented by four spaces, and each closing brace on its own line. Rules inside @media are not indented a further level.
  • JavaScript: a line break after every opening brace, semicolon, comma and opening parenthesis. Nesting is not tracked, so an if inside a function sits at the same depth as the function.
  • HTML: every opening and closing tag starts on a new line, with no indentation. The text inside an element stays on the line of its opening tag.
  • SQL: a line break before keywords such as SELECT, FROM and WHERE. Keywords are matched inside words as well, so this mode is unreliable. Use the SQL Formatter on this site for queries.
  • Minify: the same in every mode. Each run of spaces, tabs and line breaks becomes a single space. Comments stay, and nothing is renamed.

Worked example: reading minified CSS

CSS is where the pattern approach works best, because stylesheets are a flat series of rules. Paste this single line with the language set to CSS:

.container{display:flex;margin:0;padding:10px}

Format returns each declaration on its own line:

.container {
    display:flex;
    margin:0;
    padding:10px
}

Notice what stays the same. There is still no space after each colon, and the last declaration still has no semicolon, because the tool only adds line breaks around braces and semicolons. The result is valid CSS with the same meaning, as long as no value contains a semicolon inside quotes.

Worked example: JSON from an API

API responses are often sent without any whitespace. With the language set to JSON, this input:

{"name":"John","age":30,"active":true}

becomes:

{
  "name": "John",
  "age": 30,
  "active": true
}

Because the text is parsed and rebuilt, a few details can differ from the original. Keys that look like whole numbers move to the front in ascending order, so {"b":1,"2":1} comes back with "2" first. If a key appears twice, only the last value survives. Numbers are held as 64-bit floats, so 1.0 becomes 1, and an integer above 9,007,199,254,740,991 can change: 9007199254740993 comes back as 9007199254740992. If your data carries large IDs as bare numbers, check them before relying on the output.

Worked example: where JavaScript mode falls short

JavaScript has too many contexts for simple patterns to handle. Take this input:

var s="a;b,c";if(x){go();}

The output is:

var s="a;
b,
c";
if(
    x) {

    go(
    );
}

The first three lines show the problem. The semicolon and comma inside the string were treated as code, so the string now spans three lines, and a JavaScript string in double quotes cannot contain a raw line break. Running this would be a syntax error. The output is still useful for working out what a minified script does, as long as you read it and never paste it back into your project.

Formatting, minifying and obfuscating are different jobs

Formatting changes the layout of code for people. A production minifier such as Terser or esbuild goes much further than the Minify button here: it parses the code, removes comments, shortens local variable names and can drop code that never runs. Obfuscation deliberately makes code hard to follow. Formatting loses nothing, but a minifier throws away the original names, so no formatter can bring them back. The way to recover them is a source map, a separate file produced at build time that links the minified code back to the source.

The Minify button here only collapses whitespace, so the saving is modest. A small CSS rule written across three lines with two-space indentation is 20 characters, and Minify brings it to 18. Most web servers also compress text with gzip or Brotli, which already shrinks repeated whitespace very well, so real build tools are worth using when download size matters.

Common mistakes

  • Using Minify on JavaScript with // comments. A line comment runs to the end of its line, and after collapsing there is only one line. // note followed by var a = 1; turns into a single comment, and the code after it no longer runs.
  • Using Minify on text where spacing matters. Two spaces inside a string, or the contents of an HTML pre element, are reduced to one.
  • Formatting HTML that relies on inline spacing. A line break next to an inline tag such as b or span displays as a space, so words that were joined can separate. Inside pre and textarea, the new line breaks show up as actual lines.
  • Assuming no error means valid JSON. Invalid input comes back unchanged, so an output that looks exactly like your input is the signal that parsing failed.
  • Running SQL through SQL mode. ORDER BY comes out as OR DER BY, and a column called brand is split around the letters and. The output will not run.

When to use something else

For code you are going to keep, use a formatter that parses the language. Prettier covers JavaScript, TypeScript, CSS and HTML, and by default uses two-space indentation and an 80-character line width. Adding a .editorconfig file to a repository lets most editors agree on indent size and line endings, so a whole team produces the same layout. Run the formatter on save or in a pre-commit hook, and make a single commit for any bulk reformatting so that real changes stay easy to find in the history.

This page is the right fit for the quick job with nothing installed: looking inside a minified file, a snippet from a log, or a stylesheet copied from a browser. For JSON with error reporting, use the JSON Formatter. For queries, use the SQL Formatter. To compare two versions, format both first and then open the Text Diff tool, so layout differences do not bury the real ones.