JS Formatter

Formatted lines—
Statements—
Output length (chars)—

How JS Formatter is calculated: Basic indent formatter for standard JavaScript — braces open blocks, semicolons end statements, one level of indent per block. It does not parse the code, so unusual syntax may format oddly.

What is a JS formatter?

A JS formatter takes JavaScript that has been squeezed onto as few lines as possible and prints it out the way a human would write it: one statement per line, blocks indented to show their nesting, and braces lined up so the structure is visible. The code does the same thing after formatting — only its layout changes.

The need is obvious the first time you open a minified file. A library downloaded from the web, a script copied from a live page, or a snippet pasted from a chat message often arrives as a single long line, or a handful of lines with no indentation at all. Reading it is miserable, and editing it is worse. A formatter turns that wall of text back into code you can follow.

The default example is a small greeting function written without any spacing: function greet(name){return 'Hello, '+name+'!';}greet('Ada');. It is 62 characters on one line. Formatted with two-space indentation it becomes 4 lines and 67 characters — the return statement indented inside the function, the closing brace on its own line, and the call to greet on a line by itself.

This is a basic indent formatter for standard JavaScript, and the formula line says so plainly. It understands braces, semicolons, strings, and comments — the everyday structure of normal code. It does not parse the language the way a full formatter does, so genuinely unusual syntax can format oddly. For the code most people paste in, it does the job well.

How this JS formatter works

The formatter reads the code character by character, keeping track of two things: whether it is inside a string, and how deep it is inside braces. A string — single quotes, double quotes, or backticks — is copied through untouched, so a brace or semicolon inside a quoted message is never mistaken for code structure. The greeting 'Hello, ' keeps its space and its comma exactly as written.

Outside strings, an opening brace starts a new block. The formatter finishes the current line with the brace, moves to a new line, and increases the indent level by one. A closing brace does the reverse: it finishes the current line, drops the indent level by one, and puts the brace on its own line. A semicolon ends a statement, so the statement gets its own line at the current indent level.

Comments are handled as text, not structure. A single-line comment is copied to the end of its line, and a block comment is copied through whole, so neither can confuse the brace counting. That matters, because a comment that contains a brace — and people do write those — would otherwise throw the indentation off for the rest of the file.

One nicety is built in for the most common pattern: else. When a closing brace is followed by else, the formatter keeps them on one line — } else { — instead of splitting them apart. An if with two branches formats to 5 lines: the if line, the first statement, the } else { line, the second statement, and the closing brace. It reads the way a person would write it.

The numbers: lines, statements, and length

Three numbers describe the formatted result. Lines is the count of lines in the output — 4 for the default example. Statements is the count of semicolons in the input, which is a rough count of the statements in the code: 2 in the default, the return inside the function and the call to greet afterwards. Output length is the character count of the formatted text.

The statement count is a useful check. If you paste a function and the statement count is far higher than you expected, the code probably has more going on than you thought — or the semicolons inside a for loop header are being counted, which they are, since this is a simple counter and not a parser. Treat it as a rough guide, not an exact statement count.

Formatted code is usually a little longer than the input, because formatting adds newlines and indentation. The default is 62 characters compressed and 67 formatted — barely longer, because it was already short. A long minified file can grow by 20 or 30% when formatted, which is the price of readability and a price worth paying while you are reading it.

When you are done reading, the flow reverses: a minifier shrinks the code again for delivery. Format to read and edit, minify to ship. The two tools are a pair, and this page's sibling minifier does the shrinking with the same string-aware care this formatter uses for the expanding.

A worked example: a greeting function, formatted

Take the default code: function greet(name){return 'Hello, '+name+'!';}greet('Ada');. It is one line, 62 characters, and it does two things: it defines a function, and it calls that function with the name Ada. On one line, those two things blur together.

Formatted with two-space indentation, the result is 4 lines. The first line is function greet(name) { — the definition, with its opening brace. The second line is the return statement, indented by two spaces: return 'Hello, '+name+'!';. The third line is the closing brace. The fourth is the call: greet('Ada');. The output is 67 characters.

Run the formatted version and it returns Hello, Ada! — exactly what the compressed version returns. Nothing about the logic changed, only the layout. That is the whole point of a formatter: same behaviour, readable form.

Try the if-else example to see the else handling: if(x>1){y=2;}else{y=3;}. It formats to 5 lines, with the middle line reading } else { — the two branches stay visibly joined, the way a person would write them, instead of the closing brace and the else drifting onto separate lines.

Who formats JavaScript, and why

Developers format JavaScript whenever they inherit code they did not write. A library downloaded for inspection, a script copied from a production page, or a snippet from a colleague's message all arrive compressed or inconsistently spaced. Formatting first means reading the code in a consistent layout, which is the fastest way to understand what it does.

Debugging is the other big use. When a script misbehaves, the first step is usually to read it, and reading a single long line is slow and error-prone. Formatted, the nesting is visible: which statements sit inside which function, which branch of an if is running, and where a block actually ends. Many bugs are found in the first minute of reading formatted code.

Learners use a formatter to check their own code. A beginner writing JavaScript in a plain editor often loses track of braces, and a formatted view shows the structure back to them — if the indentation looks wrong, the braces are wrong. It is a quicker diagnosis than counting braces by hand.

Anyone who copies code from the web uses a formatter too. Code in a blog post or an answer on a forum is often compressed to save space, and pasting it into an editor as-is makes it hard to adapt. Format it, read it, change what you need, and only then paste it into your own project.

JS formatter vs a full code formatter

The honest difference is parsing. A full formatter reads the code into a syntax tree, understands every construct in the language, and can reprint it perfectly — including tricky cases like object literals, chained calls, and template strings with expressions inside them. This tool does not parse. It tracks braces, semicolons, and strings, which covers the everyday structure of standard JavaScript, and it says so in the formula line.

For most pasted code, the simple approach is enough. Functions, if statements, loops, and object literals all follow the brace-and-semicolon pattern this formatter understands. Where it can struggle is unusual syntax: a semicolon inside a for loop header is counted as a statement end, and a template literal containing a brace is handled only as far as the string tracking reaches. If the output looks wrong, that is usually why.

The trade is that this formatter needs no setup, no installation, and no build step. A full formatter lives in an editor or a project toolchain and is the right choice for a codebase you work in every day. This page is the right choice for a snippet you need to read right now, once.

For data instead of code, the JSON formatter does the same job for JSON, and for plain text the text formatter tidies spacing without understanding any grammar. For the opposite direction — shrinking formatted code back down — the JS minifier in this group removes comments and whitespace with the same string-aware care. If you need to measure a snippet, a word counter counts its words and a character counter counts its characters.

Common JS formatting mistakes

The first mistake is formatting code with a syntax error and expecting the formatter to fix it. A missing brace makes everything after it indent one level too deep, and a missing semicolon can join two statements onto one line. The formatter shows the structure as written, mistakes included — which is useful, because the place where the indentation goes wrong is usually where the error is.

Second: assuming formatted code is correct code. Formatting only changes layout. A variable spelled two different ways, a comparison using one equals sign instead of two, or a function that is never called all format perfectly and still misbehave. Read the formatted code, then run it, and trust the run more than the layout.

Third: reformatting by hand after the tool has done its job. If you edit the formatted output and add a block with different indentation, the file drifts back into inconsistency. Pick one indent size, set your editor to match, and reformat after edits instead of guessing.

Fourth: pasting code with secrets in it and forgetting the output is still code. A formatted API key is just as much an API key as a minified one. This page runs entirely in your browser and uploads nothing, but the moment you paste formatted code somewhere else, the same care applies as with any other code.

How to use this JS formatter

Paste your JavaScript into the box at the top. The default example is already there: a one-line greeting function that formats into 4 lines. Replace it with your own code and the formatted result appears instantly — there is no button to press.

Choose two-space or four-space indentation from the dropdown. Two spaces is the default and the most common style in modern JavaScript. Four spaces gives deeper nesting that some people prefer for complex code. Switching reformats the same code immediately, so you can compare before you copy.

Copy the formatted code from the output box. Check the line and statement counts first: if the line count looks far too high, or the indentation stops making sense halfway down, look for a missing brace or an unclosed string in the input — those are the two things that throw a simple formatter off.

Everything runs in your browser. Your code never leaves your device, is never uploaded, and is never stored. You can format proprietary code, half-finished drafts, or work code without worrying about where it goes — it goes nowhere, and closing the page removes it. Once the JavaScript is tidy, keep the repo tidy too — our gitignore generator builds the ignore file for a Node project in one click.

Frequently Asked Questions

No. Formatting only changes whitespace and line breaks, which do not affect how JavaScript runs. The default example returns Hello, Ada! whether it is on one line or four. What changes is readability — the structure becomes visible.

Roughly one line per statement, plus two lines per block for the opening and closing braces. The default function formats to 4 lines, and an if-else with two branches formats to 5 lines. A long minified file can grow by 20 to 30% in length when formatted.

Yes, for standard code. Strings in single, double, or backtick quotes are copied through untouched, so a brace or semicolon inside a message is never treated as code structure. Comments are copied as text too, so a brace inside a comment cannot throw the indentation off.

No — it is a basic indent formatter, and the formula line says so. It tracks braces, semicolons, and strings rather than parsing the language, so unusual syntax can format oddly. For everyday functions, conditions, and loops it works well, and for a whole codebase an editor's full formatter is the better tool.

More free tools

More calculators and utilities from Sortrun — free, no sign-up.