JavaScript Formatter

Paste JavaScript that is minified, copied from a browser or just badly indented, and the tool lays it out with one statement per line and indentation that follows the braces. It reads the code with a proper tokenizer, so strings, template literals, regular expressions and comments are left exactly as written. Choose the indent size and, if you like, switch string quotes to single or double where that is safe. It never adds or removes semicolons.

What you enter

What you get

How it works

  1. Paste your JavaScript into the box. It is processed in your browser and never uploaded.
  2. Choose the indent: 2 spaces, 4 spaces or a tab. Choose whether to keep your string quotes, or switch plain strings to single or double quotes.
  3. Read the formatted code in the result. Blocks are indented, each statement starts a new line, and short objects stay on one line.
  4. Copy the result or download it as a file. Compare it with the original in your editor if you want to confirm only whitespace changed.

What good formatting gives you

Indentation is a map of the program's structure. When every block is indented one step deeper than the code that contains it, you can see at a glance where a function starts and ends, which lines belong to an if and which to its else. When a file is on one long line, none of that is visible, and a missing brace can take an hour to find.

Consistent style also keeps changes small. If everyone on a project formats the same way, a code review shows only the lines that really changed instead of a wall of whitespace differences. Many teams run a formatter automatically on every save for exactly this reason.

Reading code that was minified

Minified code is valid JavaScript with all the optional whitespace removed, and often with names shortened to single letters. A formatter restores the layout but not the names, so you will see something like function a(b,c){return b+c} become a function named a with parameters b and c. Reading it is far easier than reading one line, but you still have to work out what the short names mean from how they are used.

If a site publishes source maps, your browser's developer tools can show the original code. Where it does not, formatting the file and searching for string constants or property names is the best way in, because those survive minification. Always respect the licence of code you read, and do not copy code that you are not allowed to reuse.

Tips

Frequently asked questions

What does a JavaScript formatter change?

Only whitespace: line breaks, indentation and the spaces around operators and punctuation. The tokens themselves, meaning the names, numbers, strings, operators and comments, stay in the same order. With the quote option on, a plain string may switch from one quote style to the other.

Will it change what my code does?

It should not. It never adds or removes semicolons, never reorders anything and keeps a line break wherever the language treats it as the end of a statement, for example after return. Even so, run your tests after formatting a file you depend on.

How are strings and regular expressions protected?

The tool reads the code token by token. Everything inside a string, a template literal, a regular expression or a comment is one unit that is copied through unchanged, so a // inside a string is not mistaken for a comment and a / inside a pattern is not mistaken for a division.

Why do some objects stay on one line?

A short object with up to three properties, no nested objects or functions and no line break after its opening brace is kept on one line, for example { a: 1, b: 2 }. Larger or nested ones are spread over several lines.

Does it work with modern syntax?

Yes for plain JavaScript: arrow functions, classes, async and await, template literals, spread, optional chaining, nullish coalescing and modules. It does not read JSX or TypeScript type annotations, and code that does not parse as JavaScript may be laid out oddly.

Related: JavaScript minifier, JSON formatter and validator, Regex tester.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Free Tools

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap