About JS Formatter
Format and beautify JavaScript (including ES6+/ES2023) code in your browser with configurable indentation (2, 4, 6, or 8 spaces).Handles all modern JavaScript syntax. Arrow functions, async/await, classes, template literals, destructuring, spread operators, optional chaining, nullish coalescing, regular expressions, and JSX. Toggle between beautify mode (clean, readable indented output) and minify mode (compact output for production bundles).Powered by a custom parser that preserves comments and maintains syntactic correctness. Ideal for debugging minified scripts, standardizing team code style, or preparing code snippets for documentation. For example, minified JavaScript is re-indented by statements and scope with syntax highlighting, making errors easier to locate. A common workflow: normalize a messy legacy bundle with this tool before running a linter, so your diffs stay clean and reviews stay fast. Automatic semicolon insertion is where formatting most easily changes behaviour, and understanding it keeps formatting from becoming a bug fix. At certain line breaks the parser inserts a semicolon whose position may differ from intent, so splitting a line or moving content to the next line can change the program semantics. A formatter should never alter behaviour and must preserve or explicitly punctuate such positions. A line break after a few keywords changes meaning, another place needing care. When a break falls between a keyword and its expression, the insertion rule can end the statement early so the returned value or the expression differs entirely from expectation. Keep the keyword and its expression on one line, or write the structure explicitly with parentheses and semicolons. Comma and bracket placement affects parsing in argument lists and in array and object literals alike. A leading comma parses in most cases yet interacts with the insertion rule in some positions to surprising effect, and a bracket on its own line may be read as a separate expression container. Keep commas at the end of a line and an opening bracket on the previous one. How comments are preserved decides whether the formatted code still works, since some carry functional roles. Certain comments are read by tooling that generates code or changes behaviour, so moving or merging them silently breaks the feature, and formatting examples inside comments can be wrecked by reindentation. Preserve comment placement, especially directly above a statement. Modern syntax adds complexity because its rules often exceed standard grammar. Template syntax and component markup mixed with code indent differently, decorators with classes have positional requirements, and experimental syntax is unevenly supported across tools. Use a formatter that explicitly supports the syntax rather than a generic parser. Finally, keep formatting and semantic repair separate, the pair most often confused. Formatting changes whitespace and line breaks only; it must not insert or remove semicolons, change comparison operators or alter evaluation order. If a tool does those, it is also rewriting code. Validate with a piece of boundary-case code before adopting it.
How It Works Internally
Formatting re-lays out code from a syntax tree, so it must truly parse rather than process line by line. That surfaces syntax errors for free, at the cost that rules such as automatic semicolon insertion make removing newlines semantic — joining a line starting with return or throw onto the previous one changes the returned value. Confirm no newline-dependent constructs first.
return
{a: 1}Newline kept → returns undefined | Joined into one line → returns {a:1} (ASI changes the meaning)Measured on This Page
| Fixed: line comment swallowed the next line | Without a newline after // keep me, the following const x = 1; was glued on as '// keep meconst x = 1;' — an entire statement commented out, while node --check still passed because a comment is legal. Line comments now own their line |
|---|---|
| Syntactically valid | All four outputs pass node --check, so formatted code still parses |
| Literals intact | The template literal ${1 + 2} and the regex /a\/b/g survive verbatim, neither escaped nor split |
| Comments kept | Line and block comment text is preserved, with block comments staying in place |
A formatter's top priority is leaving semantics alone, and semantic damage usually raises no error — this 1 defect was what happened here: one missing newline after a line comment merged the next statement into it, node --check still passed, and at runtime a statement simply stopped running. Only behavioural comparison catches that (run before and after, diff the output); checking whether the code merely looks tidy cannot. Note also that this is a lightweight beautifier built on token scanning plus indent reconstruction, not a full parser, so JSX, decorators and optional chaining get conservative treatment — reach for Prettier on complex code.
How to Use
- Open "JS Formatter"
- Paste or type JavaScript source
- Switch between "Format" and "Minify"
- Choose 2 or 4 spaces when formatting
- Click the copy icon in the output header
Use Cases
- Scope & function cleanup — Expand minified JS by function and scope level with clean indentation for a clear view of closures and callback chains.
- Project standards — Standardize JS code indentation and formatting across your project, cutting review noise.
- Code review — Format JS code before review to make logic structure and changes clearer.
- Minify production code — Switch to minify mode to strip comments and whitespace for smaller build artifacts.
- Debug minified scripts — Restore a one-line CDN script to readable structure and quickly locate the failing point.
FAQ
Does formatting change code logic?
No. Formatting only adjusts indentation and line breaks. JavaScript execution logic, variable names, and structure remain unchanged.
Does it support ES6+ syntax?
Yes. Properly handles arrow functions, template literals, destructuring, async/await, optional chaining, nullish coalescing, and other ES6+ through ES2023 features.
Does it support minification?
Yes. Switch to minify mode to remove comments and excess whitespace for minimized JS output suitable for production builds.
Does it handle regular expressions?
Yes. The lexer distinguishes regex literals from division operators, so /pattern/ is not misread and formatting stays correct.
Can it process TypeScript?
It handles TypeScript type annotations and structure formatting well as a basis for cleanup; for full TS type-aware transforms use a dedicated tool.
Does minifying change semantics?
No. Minifying removes comments, whitespace and line breaks only; variable names and logic are preserved. For aggressive renaming use a dedicated build mangler.
Behaviour changed after formatting (a stray return)?
Safe formatting is semantics-preserving; visible changes almost always come from ASI boundaries, e.g. a return whose value sat on the next line being joined. Run tests before and diff after — the only reliable verification, no tool can infer intent.
Can it format minified one-line code?
Yes, that is a primary use case. Minified code regains readable multi-line structure, but variable names stay mangled (a, b, c) because renaming requires scope analysis — that is deobfuscation, not formatting.