Format JavaScript
The status line checks the code as you type and never changes your input. Live checking runs for inputs under 300,000 characters; longer inputs wait for the Format button. Formatting only moves whitespace around: it never adds a semicolon, never renames anything, and never runs your code. Minify adds a semicolon only after return, throw, break or continue on its own line, to keep the meaning the same. Template literals come back exactly as written, including the expressions inside ${...}.
What the formatter changes
- Indentation. One level per nesting depth, with 2 spaces, 4 spaces or a tab. A short object or array stays on one line when it fits in 80 characters; a longer one breaks to one member per line.
- Minify. The reverse: comments go, whitespace collapses, and the semicolon before a closing brace and at the end of the file drops. The status line reports the bytes before and after, and the percent saved.
- Semicolons. Left exactly as written. The formatter never adds one, and never removes one except the redundant one before } in Minify mode.
- Strings, regexes and template literals. Printed exactly as written. A string that contains ;} stays intact, and a regex keeps its flags.
- Comments. Kept in Formatted mode, dropped in Minify mode. A comment between two words leaves one space behind, so typeof followed by a comment and then x still reads as typeof x.
- Blank lines. Kept, at most one in a row. The formatter never invents a blank line where the source had none.
What the syntax check catches
Formatting doubles as a syntax check: the page compiles the code with new Function, which parses it without running it. A fetch() call or a while (true) loop in your input is read, not executed. The check reports three things, and skips one case.
Exact error position
When the code fails to parse, the page reports the message and an estimated line, found by testing shorter prefixes of the file. For a minified file on one line, the line number cannot narrow it down, and the page says so instead of guessing.
What it does not catch
Type errors, wrong variable names, and anything that only fails at run time. A file can pass this check and still throw when it runs. The check answers one question only: does this parse.
Import and export
A file with import or export statements skips the syntax check, because new Function cannot parse module code. Formatting still works: the tokenizer reads import and export the same way it reads any other statement.
ASI warnings
The page also warns about two patterns that read one way and run another. A line that starts with (, [ or a backtick joins the line above it into one expression, so a missing semicolon silently calls the line above. And return on its own line returns nothing, because the value on the next line is never reached.
Regex or division
The slash is the hard part of formatting JavaScript. In const re = /ab+c/g, the slash starts a regex. In const d = 4 / 2, it divides. The formatter tells them apart by what came before: after =, (, a comma, return or an operator, a slash starts a regex; after a value, it divides.
One case stays ambiguous: the slash after a closing brace. if (x) { y(); } /re/.test(s) reads the slash as a regex, which matches how most beautifiers decide it. If your code divides right after a block, put the division on its own line or wrap it in parentheses, and the formatter reads it as division.
| Code | Reads as |
|---|---|
const re = /ab+c/g; | Regex, after = |
const d = 4 / 2; | Division, after a value |
if (x) { y(); } /re/.test(s); | Regex, after } (the ambiguous case) |
return /re/.test(s); | Regex, after return |
const q = a / b / c; | Division twice, after values |
Format vs Prettier
This page and Prettier solve different problems. This page is the quick check for a snippet you are reading right now; Prettier is the formatter for a whole project. The table shows where each one fits.
| This page | Prettier | js-beautify | Terser | |
|---|---|---|---|---|
| How it reads code | Tokenizer | Full parser | Tokenizer | Full parser |
| Needs install | No | Yes, CLI or editor | No, runs in the browser | Yes, CLI |
| Rewrites code | No, whitespace only | Yes, one style | No, whitespace only | Yes, renames and drops code |
| Adds semicolons | No | Optional | No | No |
| JSX and TypeScript | No | Yes | Partial | No |
| Minify | Yes | No | No | Yes |
| Best for | A snippet, right now | A whole project | A snippet, right now | Shipping a bundle |
Common JavaScript errors
| Error | What it usually means | Fix |
|---|---|---|
Unexpected token '}' | A closing brace with no opening brace, or a missing value before it | Count braces from the reported line upward |
Unexpected end of input | A brace, bracket or quote never closed | Check the last block you opened |
Unterminated string literal | A quote opened on one line and never closed on that line | Close the string, or use a template literal for multi-line text |
Invalid regular expression | A slash read as a regex that never closes, often a division after } | See the regex or division section above |
Missing semicolon, ASI gotcha | A line starting with ( or [ joined the line above | End the previous line with a semicolon |
return on its own line | The value sits on the next line, so the function returns undefined | Put the value on the same line as return |
Frequently asked questions
Does this page run my code?
No. The syntax check parses your code with new Function, which compiles it without running it. A fetch() call or a while (true) loop in your input is read, not executed. Nothing is uploaded either: the formatter runs in your browser.
Does formatting change what my code does?
No. Formatting only changes whitespace, line breaks and indentation, and tokens are never added, removed or reordered. One exception: in Minify mode, a return, throw, break or continue on its own line gets a semicolon added, because without it the minified code would return a value the original never returned.
Can it format JSX or TypeScript?
No. JSX reads as comparisons, so a div tag breaks the parse, and TypeScript annotations such as x: string read as labels. For those, use Prettier, which has a real parser. This page handles plain JavaScript only.
Can it un-minify production code?
Yes, up to 500,000 characters. The layout comes back: indentation, line breaks, one statement per line. Variable names stay minified, because the original names are gone for good.
Why does Minify report byte savings?
It counts the bytes before and after, and the percent saved. The gzip estimate comes from your browser's CompressionStream when it is available, and is marked as a rough estimate when it is not.
How is this different from Prettier?
Prettier parses your code into a syntax tree and prints it again, so it can rewrap lines and enforce one style. This page reads tokens only, so it keeps your line breaks where they matter and never rewrites your code. It is the quick check for a snippet; Prettier is the formatter for a whole project.
Sources
Prettier, Prettier documentation: the reference formatter, and the comparison in the table above.
MDN, JavaScript Guide: statements, expressions, and how automatic semicolon insertion works.
MDN, Function constructor: new Function compiles code without running it, which is how the syntax check works.
js-beautify, js-beautify on GitHub: the tokenizer approach this page follows.
Terser, Terser on GitHub: the minifier most build tools use, and the comparison in the table above.
Agents
Machine readable summary for AI assistants: