JavaScript Formatter and Beautifier

Re-indent and re-space JavaScript code.

Runs in your browser. Nothing is uploaded — your input is processed on this device and discarded when you close the tab.

JavaScript Formatter and Beautifier

How to use this tool

  1. Paste JavaScript. Functions, objects, loops and conditionals are all handled.
  2. Format. The code is re-indented two spaces per level and re-spaced, with for-loop headers kept on one line.
  3. Copy the result back into your file, and test it before shipping.

Worked example

Paste the sample: function total(items){let sum=0;for(let i=0;i<items.length;i++){sum+=items[i];}return sum;}. The formatted output puts the function body indented one level, the for-loop header on one line, the sum line indented two levels, and the return statement indented one level before the closing brace.

What this tool does not do / Limits

This formatter re-indents and re-spaces tokens; it does not parse or validate code, so it cannot tell you whether the code runs, and unusual but valid syntax can still come out imperfectly. In particular, a regular expression in an ambiguous position is guessed from the token before it, and object literals and code blocks are treated the same way for indentation. Always run formatted code before relying on it.

Frequently asked questions

Does formatting change what the code does?

It should not, because only whitespace is changed. Strings, template literals, comments and regular expressions are kept exactly as written.

What if my code has a syntax error?

The formatter will still re-indent what it can, but it will not point out the error. Use a proper parser or linter for that.

Related tools