What the JavaScript Beautifier does
The JavaScript Beautifier formats JavaScript with Prettier 3.3.3, an open-source code formatter, running. It supports JavaScript, including modern syntax and JSX.
JavaScript Beautifier examples
The built-in sample
Input
function hi(n){return n>1?"many":"one"}Output
function hi(n) { return n > 1 ? "many": "one";
}
This is the input that the Load sample button fills in.
Quotes, semicolons and arrow functions
Input
const a = {'b': 1, c: 'it\'s'}
if(a)b
let f = x => x*2Output
const a = { b: 1, c: "it's" };
if (a) b;
let f = (x) => x * 2;
Strings become double-quoted, which also removes the escape in it's. Unneeded quotes around the key b are removed, missing semicolons are added and the arrow function parameter gets parentheses.
Each output was produced by running this tool on the input above it.
Details and limits
- Modern syntax is understood, including optional chaining, private class fields, top-level await and JSX.
- Blank lines you left between statements are kept, but several in a row are reduced to one.
- Lines are wrapped at 80 characters where the syntax allows it. Long strings are never split.
- Formatting does not change what the code does. It does not rename variables, remove unused code or minify.
Common errors
| Message or symptom | What it means |
|---|---|
| SyntaxError with a line and column | The code does not parse. Check brackets, quotes and that you chose the right formatter for the language. |
| Unexpected token in a type annotation | TypeScript or Flow syntax was pasted into the JavaScript formatter. Use the TypeScript or Flow formatter. |
Questions
Can it format minified code?
Yes. The code is parsed and printed from its structure, so a one-line bundle comes back indented. Names that were shortened by a minifier stay short.