JavaScript Beautifier
Add indentation and line breaks to JavaScript source text.
Your result will appear here…
How to use JavaScript Beautifier
- Paste JavaScript source or compact code.
- Select Format.
- Read the indented result.
Example
Input / settings:
const greet=(name)=>{return `Hello, ${name}!`;};Output:
const greet = (name) => {
return `Hello, ${name}!`;
};Formatting the output again returns the same text.
When to use
- Read a minified or one-line script.
- Tidy a snippet before you put it in documentation.
- Make the structure of unfamiliar code visible.
Overview
JavaScript Beautifier re-indents JavaScript with a lightweight tokenizer: two-space indentation, one statement per line, spaces around operators, } else { on one line, indented switch cases and one property per line in object literals. It does not add or remove semicolons, wrap long lines, reorder code or check that the program is valid. Strings, template literals, regular expressions and comments are kept as written.
Supported input
JavaScript text of at most 200,000 characters. JSX is not supported and TypeScript annotations are formatted as plain text.
Output details
Indented JavaScript with two-space indentation. Semicolons are not added or removed and long lines are not wrapped.
Common mistakes
- Semicolons are neither added nor removed. Code written without semicolons stays that way, and the line breaks it depends on are kept.
- Long lines are not wrapped. A call with many arguments, a long chain such as promise.then(a).catch(b).finally(c), and an array all stay on one line.
- Object literals are always expanded: {a: 1, b: {c: 2}} becomes one property per line, while an empty {} stays as it is.
- Template literals are not formatted. Code inside ${ } stays exactly as typed, including odd spacing such as ${ b+c }.
- The style is fixed and is not Prettier's or ESLint's. There are no options for indentation, quotes or line width, so the output can differ from your project's formatter.
- A regular expression statement right after a closing brace is read as division. After function f(){}, /a +b/.test(s) is formatted as / a + b / .test(s), which is a different pattern. Put a semicolon in front of it.
- JSX is rejected: <div>don't</div> returns A string is not closed. TypeScript annotations are formatted as ordinary text, so const x: number = 1 comes back unchanged.
Practical tips
- Format minified code to read it, then compare its structure with the source.
- The tool changes only whitespace and line breaks, so run your usual tests if you paste the formatted code back into a project.
Privacy and security
This tool runs entirely in your browser. Your input is never uploaded to our servers. Formatting runs locally in your browser and nothing is uploaded. Your source is only read as text; nothing in it is executed. An unclosed string, template literal, comment or regular expression is rejected rather than changed, but the tool is not a parser and does not check that the code is valid JavaScript.
References
- MDN: Lexical grammar
- ECMAScript Language Specification: Automatic Semicolon Insertion
- MDN: Template literals
Frequently asked questions
Does JavaScript Beautifier change what my code does?
It is meant to change only whitespace and line breaks. Because it is a lightweight tokenizer and not a parser, check results for rare constructs such as a regular expression right after a closing brace.
Which indentation does the beautifier use?
Two spaces per level. The page has no indentation setting.
Can it format TypeScript or JSX?
No. TypeScript annotations pass through as plain text, and JSX text that contains an apostrophe is rejected as an unclosed string.