JavaScript Formatter

Format and beautify JavaScript in your browser — syntax checked first with precise error locations, strings, regexes and comments left byte-identical, nothing uploaded. Free, no sign-up — runs locally in your browser.

Your work is auto-saved in this browser only, so a refresh won't lose it. Nothing is uploaded.

…or drop a file onto the box above. Files are read locally, never uploaded.

What is a JavaScript formatter?

JavaScript arrives in unreadable shapes from every direction: minified bundles with a whole library on one line, snippets copied out of browser consoles, code generated by other tools, a colleague's file where the indentation changes philosophy every ten lines. A JavaScript formatter (or beautifier) rewrites that code with one consistent layout — every block indented by the same step, every function and loop opened up onto its own lines — so the structure of the program is visible again and the logic can be reviewed, diffed and debugged.

A formatter is only trustworthy if it understands what it is moving. JavaScript is full of text that looks like structure but is not — braces inside strings, a } in a comment, a regular expression that a naive tool reads as division — and full of places where a line break is not merely cosmetic, because automatic semicolon insertion gives some newlines a meaning. That is why this tool parses your code before it formats it, with a real ECMAScript parser: the layout is computed from the program's actual grammar, not from counting braces.

How to use this tool

  1. Paste your JavaScript into the box above (or press Load sample to see a worked example). You can also upload a .js file or drop one onto the box — the file is read in your browser and never sent anywhere.
  2. Choose an indent — 2 spaces is the community default; 4 spaces and tabs are one click away.
  3. Press Format. If the code parses, you get the reformatted version and a line count. If it does not, you get the line and column of the first syntax problem — fix that one and format again.
  4. Tick Auto-format as I type if you would rather not press the button: the result updates as you edit (very large inputs are still formatted on demand, to keep typing smooth). While the code is mid-edit and does not yet parse, the message line says so and your last good result stays on screen.
  5. Use Copy result or Download result to take the formatted code with you. Your input above is never modified — the result is a separate copy, and nothing you paste is ever executed.

Syntax checked first — and why that protects your code

Before formatting, the tool parses the input twice over: once as an ES module, so import, export and top-level await are accepted, and once as a classic script, so script-only constructs are accepted too. If neither grammar accepts the text, you get the more plausible error — the parse that got further into your code — with its exact line and column. Only code that parses under one of the two grammars is ever reformatted.

That gate is what makes the layout safe to apply. A parser knows that the slashes in let rate = total / count are division while the slashes in /^https?:\/\// delimit a regular expression; that a template literal can contain a whole nested expression without its braces becoming blocks; and that a return followed by a line break is a statement with no value, so the break must be kept. Text-level beautifiers get these wrong in ways that silently change behaviour. This one cannot reformat what it could not parse, and says so instead.

What formatting never changes

Layout only — that is the whole contract. Indentation follows the option you chose; statements and expressions are broken onto lines at their grammatical boundaries; spacing around operators and after keywords is made uniform. Everything else is your code, untouched: no renaming, no reordering, no added or removed semicolons, no quote-style conversion. String, template literal, regular expression and comment contents are carried across byte-for-byte. Blank lines survive, in moderation — up to two consecutive ones, as authored — so the paragraphing of your code survives the trip.

Examples

This compressed module:

export function cartTotal(items){let total=0;for(const item of items){total+=item.price*item.qty;}return total.toFixed(2);}

formats into:

export function cartTotal(items) {
  let total = 0;
  for (const item of items) {
    total += item.price * item.qty;
  }
  return total.toFixed(2);
}

And this snippet — note the unclosed function body — is reported at the exact position where the parser runs out of input, instead of being "formatted" into something that looks plausible and runs differently:

function total(items) {
  let sum = 0;
  for (const item of items) {
    sum += item.price;

Formatting is not minifying, and not linting

Three tools, three different promises. A minifier makes code smaller for delivery — stripping whitespace, renaming variables — and leaves it less readable on purpose. A linter has opinions: it flags unused variables, inconsistent style and probable bugs, and expects you to argue with it. A formatter only rearranges whitespace so the code you already have reads consistently. This page is the third one, and it will pass code a linter would reject with flying colours — ugly logic, beautifully laid out — as long as it is syntactically valid JavaScript.

Frequently asked questions

Is my code uploaded or executed anywhere?

Neither. Your code is parsed and reformatted in your browser, on this page, and it is never run — parsing reads the grammar, it does not execute anything. Nothing is sent to a server, and your draft is auto-saved in this browser only, for 7 days; the Clear saved draft button deletes it immediately.

Why does the tool check syntax before formatting?

Because a formatter has to understand where statements and blocks begin and end, and text it cannot parse it can only guess at — a guessed line break in the wrong place can change what the code does (automatic semicolon insertion makes some line breaks significant). So this tool parses first with a full ECMAScript parser. Valid code is formatted; invalid code gets the line and column of the first problem instead of a best-effort reformat. Fix the first error and format again — the rest is usually a cascade.

Does it support JSX or TypeScript?

No — this formatter handles plain JavaScript: classic scripts and ES modules, up to the latest ECMAScript version. JSX and TypeScript add syntax a JavaScript parser cannot accept, so a snippet using them is reported as invalid at the first token the parser cannot place. That is deliberate: pretending to format syntax it does not understand is how tools corrupt code.

What exactly does formatting change?

Indentation, line breaks, and spacing around operators, braces and keywords — layout only. Identifiers are never renamed, statements are never reordered, and the contents of strings, template literals, regular expressions and comments are carried across byte-for-byte: a "{" inside a string is data, not a block boundary, and the formatter knows it.

Does it add or remove semicolons?

No. Your semicolons — or your deliberate lack of them — are preserved exactly as written. The formatter adjusts the whitespace around your code; it does not restyle your punctuation choices, rename anything, or apply lint rules. If a missing semicolon makes the code invalid, the syntax check says where, and the decision is yours.

Can I paste a fragment instead of a whole file?

Usually, yes: a function, a class, a block of statements, an import/export module — anything that is a complete script or module on its own. Two fragments cannot work, because they are not complete programs: a bare object literal like { a: 1 } is parsed as a block of statements (which is what it grammatically is), and a lone return statement is invalid outside a function in any context. Wrap fragments in the function or assignment they came from and they format fine.

Can I open a .js file instead of pasting?

Yes — use the Upload button or drag a file onto the input box. The file is read locally by your browser (it is never uploaded to a server), its contents land in the input box, and formatting runs immediately. Download result does the same job in the other direction.

Is this a minifier or a linter?

Neither. A minifier removes whitespace and renames variables to shrink code for delivery; a linter judges style and likely bugs. This tool does one job — it makes code readable by laying it out consistently — and deliberately leaves every other property of your code exactly as you wrote it.

Related tools