JavaScript Formatter
Paste compact or untidy JavaScript and get clean, indented code. Choose the indent, brace style and whether to keep blank lines.
Este conteúdo está em inglês.
Exemplos resolvidos
A compact function
function add(a,b){if(a>b){return a-b}return a+b}const x=[1,2,3].map(n=>n*2);
function add(a, b) { if (a > b) { return a - b } return a + b } const x = [1, 2, 3].map(n => n * 2);
Two space indent with braces on the same line.
An object and an if
const user={name:"Ann",tags:["a","b"],active:true};if(user.active){console.log("Hi "+user.name)}else{console.log("Bye")}
const user = { name: "Ann", tags: ["a", "b"], active: true }; if (user.active) { console.log("Hi " + user.name) } else { console.log("Bye") }
Four space indent with braces on their own lines.
Sobre JavaScript Formatter
This JavaScript formatter turns compact or untidy code into a clear layout. Blocks are indented, operators and commas get consistent spacing, and statements go on their own lines. Use it to read a minified file, to tidy a snippet from a forum post or to make code consistent before you commit it.
Strings, template literals, regular expressions and comments are recognised and left alone, so only the layout around them changes. Pick two or four spaces or tabs, put braces on the same line or the next, keep or drop blank lines, and wrap long lines at 80 or 120 characters. The formatter changes layout only. It does not rename, reorder or fix code.
Como usar JavaScript Formatter
- Paste your JavaScript, or open a .js file.
- Choose the indent, brace style, blank line and wrapping options.
- Copy the formatted code or download it.
Usos comuns
- Reading a minified script to see what it does
- Tidying a snippet before sharing it in a review or a question
- Making a team agree on one layout when there is no automatic formatter yet
- Laying out JSON or a data object so it is easy to scan
Perguntas frequentes
Will it fix errors in my code?
No. It only changes layout. If the code has a syntax error the formatter lays out what it can, so run the code or your tests after you paste it back.
Can it restore original variable names in minified code?
No. Names that were shortened when the file was minified are gone for good. Formatting only adds line breaks and spacing.
Does it work with modern syntax?
It handles arrow functions, classes, template literals, spread, async functions and modules. Very new proposals may be laid out less neatly.
Can I format JSON with it?
Yes, valid JSON comes out with one value per line. The JSON formatter is better if you also want errors reported.