How to format JavaScript
- Paste your JavaScript into the box.
- Choose 2 spaces or 4 spaces next to Indent. The default is 2.
- Select Format JavaScript. The result appears below.
- Select Copy result to copy it, or Clear to empty both boxes.
The tool only runs when you press the button. If you switch from 2 to 4 spaces after formatting, press Format JavaScript again to see the new indent. There is no tab option.
What the formatter does to your code
| In your code | What happens |
|---|---|
| { [ ( | Line break after it. The following lines move in one level. |
| } ] ) | Goes on its own line, one level back. A comma or semicolon right after it stays on that line. |
| ; | Line break after it, including the semicolons inside a for loop header. |
| Strings in single quotes, double quotes or backticks | Copied as is, even if they contain braces, semicolons or //. |
| // and /* */ comments | Kept. A // comment ends its line. |
| Your own line breaks and blank lines | Removed. The tool rebuilds all line breaks and indentation itself. |
For example, function add(a,b){return a+b;} with 4 spaces comes out as six lines: function add(, then a,b indented, then ), {, return a+b; indented, and }. A loop such as for(let i=0;i<3;i++){sum+=i;} is split at each semicolon in the header, so it takes eight lines.
Code that can break after formatting
Because the tool does not understand JavaScript grammar, some inputs come out with a different meaning or with syntax errors. We tested each case below.
- Code without semicolons. Line breaks are dropped, so
const a = 1andconst b = 2on two lines becomeconst a = 1const b = 2, which will not run. - Arrow functions with parentheses. In
const f = (a, b) => a + b;the closing parenthesis gets its own line, so=>starts a new line. JavaScript does not allow a line break before=>, so this is a syntax error. A single parameter without parentheses, likex => x*2, is fine. - Regular expressions. Regex literals are not detected. In
/}{/gthe braces start new lines and the pattern is split. - JSX and TypeScript. There is no special mode for either. JSX braces such as
{x}are split onto separate lines like any other brace.
Code that ends every statement with a semicolon and avoids the cases above keeps its meaning. Even then, treat the output as a reading copy.
Reading minified JavaScript
Minifiers remove spaces and line breaks and often rename variables to single letters. This tool puts line breaks back, which makes it easier to find a function, follow an if and else branch, or see where an object starts and ends. It cannot bring back the original names.
If you are looking at data rather than code, the JSON Formatter is a better fit, because it also checks that the JSON is valid. For markup and stylesheets from the same page, use the HTML Formatter and the CSS Formatter.
The tool does not check syntax, minify code or add missing semicolons. It also makes no network requests, so the code you paste stays in your browser.
Common Mistakes to Avoid
Running the formatted output instead of the original
Solution:
Formatting code that relies on automatic semicolon insertion
Solution:
Expecting readable variable names back
Solution:

