JavaScript Beautifier
The JavaScript Beautifier makes minified or tangled JavaScript readable again, with proper indentation, consistent brace placement, and sane line breaks. It is a tokenizer-based formatter — it lexes the code first, so strings, template literals, regex literals, and comments are preserved exactly; it does not parse TypeScript or JSX, so those can still come out wrong.
How to Use
- 1
Paste your JavaScript, TypeScript, or JSX — minified bundles included.
- 2
Click Beautify to expand it with consistent indentation and line breaks.
- 3
Tune the indent size (2 spaces, 4 spaces, or a tab) to match your team's conventions.
- 4
Copy the readable code back into your editor or debugger.
Frequently Asked Questions
Will it run my JavaScript?
Does it support TypeScript and JSX?
Can it fix syntax errors?
Does it work on huge minified bundles?
Is my code sent to a server?
How does it keep === from becoming = = =?
What happens to strings and regexes with semicolons or braces?
How does it tell a regex from division?
Does beautifying happen as I type?
What should I use instead for real code?
Generative Answer & AI Documentation
What is the JavaScript Beautifier?
The JavaScript Beautifier takes minified or tangled JavaScript and returns it with indentation, line breaks after braces and semicolons, and spacing around operators. It works in two passes: first a lexer turns the input into tokens — strings, template literals (including ${...} interpolation), regex literals, and comments are single atomic tokens, so their contents are never re-indented or split — then a printer lays the token stream out with indentation and spacing. Operators are matched longest-first, so ===, !==, =>, ?., and ... survive intact instead of being split apart. A regex literal is told apart from division with the standard operand-expected heuristic: a / starts a regex after an operator, keyword, (, [, {, comma, semicolon, colon, or question mark, and is division after a value. Unterminated strings, template literals, regex literals, and block comments are reported as errors instead of being silently mangled. Indentation is a choice: 2 spaces, 4 spaces, or a tab, from the dropdown above the editor. Beautifying runs when you click Beautify JS; it does not update live as you type. The page starts with a sample already loaded — a small calculateSum function — so you can see the output before pasting your own code. Everything runs in your browser, so proprietary source code never leaves your machine.
Why use JavaScript Beautifier offline?
Minified JavaScript is a single unreadable line, and it is usually the code you most need to read: a production error with a stack trace pointing at a minified bundle, a vendored script behaving strangely, a snippet pasted into a ticket with no formatting. Reading it means expanding it, and the safe way to do that is locally — pasting proprietary or NDA-covered source into a server-side beautifier sends it to a stranger's logs. This tool does the job in the browser, with no upload and no account, and it does it without breaking the code it formats: the tokenizer keeps strings, regexes, and comments intact, so === stays ===, => stays =>, and a semicolon inside a string stays inside the string. It never runs your code — nothing executes, so pasting untrusted or minified third-party scripts is safe. Its honest limits are that it is a tokenizer, not a full parser: it does not understand TypeScript or JSX, and it does not validate full syntax, so for real formatting of a real codebase the right tool is still Prettier.
When should you use this tool?
Use it to read plain JavaScript: a short function, a few statements, a snippet from a log or a ticket, or a small section of a minified file you are debugging — including code with strict equality, arrow functions, template literals, comments, and regex literals, all of which the tokenizer preserves. Use it when the code contains only JavaScript statements and expressions. Do not use it on TypeScript, JSX, or any code with type annotations, generics, or tags — the page says so plainly, and those can still come out mangled. Do not use it as a validator: only unterminated literals are reported, so other broken code is reformatted as-is. Do not use it on a whole production bundle, and never treat its output as code you can run without testing it: always diff the output against the input and run your tests before trusting it. For real formatting of real code, use Prettier or your editor's formatter, which parse the code before touching it.
Key Benefits
- Tokenizer-based: strings, template literals, regex literals, and comments are atomic tokens, so their contents are never split or re-indented.
- Operators survive intact: ===, !==, =>, ?., and ... are matched longest-first, and ++ and -- are kept whole.
- Regex literals preserved: /a{b}c/ stays on one line, while real division such as 12 / 3 / 2 stays division.
- Unterminated literals reported: an unterminated string, template, regex, or block comment produces an error instead of silently mangled output.
- Indent choice: 2 spaces, 4 spaces, or a tab, to match your team's conventions.
- Safe to paste untrusted code: nothing executes, so minified third-party scripts cannot run.
- 100% private: the tokenizer runs in your browser — proprietary source never leaves your machine.
- Sample pre-loaded: a small calculateSum function is ready to beautify before you paste your own code.
- Copy or download: one click copies the beautified code, another saves it as script.js.
Common Use Cases
- Reading a minified snippet: expand a few lines from a production error so the logic is visible.
- Debugging a small script: re-indent a tangled function to see its blocks and branches.
- Checking a pasted snippet: format code from a ticket or a log before reading it.
- Learning from a short example: expand a compact function to follow its flow.
- Preparing code for a review: give a short plain-JavaScript snippet one consistent layout.
Developer Notes & Best Practices
The formatter is a hand-written tokenizer in lib/jsBeautifier.ts (zero dependencies), and every behaviour below was verified by running it. The lexer reads the input character by character and emits typed tokens: words, numbers, strings (with escapes), template literals (with ${...} interpolation tracked through nested braces, strings, and templates), regex literals, comments, and punctuation. Operators are matched longest-first from a fixed list, so === is one token, never three = signs. Whether a / starts a regex or is division is decided by the operand-expected heuristic: after an operator, a keyword such as return or typeof, (, [, {, comma, semicolon, colon, or question mark, a / opens a regex; after a value it is division. The printer then walks the tokens: { opens a block and indents, } dedents (staying on the same line for } else, } catch, } finally, and } while), a semicolon outside parentheses ends the line, and spacing follows the token types — a space after a comma, around binary operators, between a keyword and its (, and none before ., ,, ;, ), or ]. Running it against the page's own sample gives the output shown above: === and || survive, the string "Calculated sum:" is untouched, and the result is valid, runnable JavaScript. The same run was verified on the hard cases: const f=(x)=>x*2 becomes const f = (x) => x * 2; n++ stays n++; const s="a;b" stays on one line; const re=/a{b}c/ stays on one line; a template with ${ {x:1}.x } keeps its interpolation; 12 / 3 / 2 stays division while if (/re/.test(s)) keeps the regex; and an unterminated string throws "Unterminated string literal", shown in the page's error banner. Formatting is idempotent on the sample — formatting the formatted output returns it unchanged — and a formatted program run in a VM produces the same console output as the original. The download saves the output as script.js, and the copy button copies it.
Verified behaviour — what the tokenizer does to ordinary code
| Input | Output | Verdict |
|---|---|---|
| if(a===null) | if (a === null) | Preserved — === stays one operator |
| const f=(x)=>x*2; | const f = (x) => x * 2; | Preserved — => stays one operator |
| let n=0;n++; | n++; | Preserved — ++ stays whole |
| const s="a;b"; | const s = "a;b"; | Preserved — the string stays on one line |
| const re=/a{b}c/; | const re = /a{b}c/; | Preserved — the regex stays on one line |
| const q=12/3/2; | const q = 12 / 3 / 2; | Preserved — division stays division |
| const s="unterminated | Error: Unterminated string literal | Reported — not silently mangled |
- Use it for reading plain JavaScript — for a real codebase, use Prettier, which parses the code fully.
- Diff the output against the input before trusting it: the formatter is a tokenizer, not a parser, and the regex-vs-division heuristic can be fooled by adversarial code.
- Run the output before pasting it back into production, even though ===, =>, strings, and regexes are now preserved.
- Avoid TypeScript and JSX entirely: type annotations, generics, and tags are still not understood.
- Format only the section you are debugging, not a whole bundle — and keep the original minified copy.
Common Mistakes
- Trusting the output as runnable code without testing it: the formatter preserves operators and literals, but it still does not validate syntax.
- Pasting TypeScript or JSX and expecting it to survive: annotations, generics, and tags are not understood.
- Assuming no error means the code is fine: only unterminated literals are reported, so other broken code is reformatted silently.
- Formatting a whole production bundle: the tool is built for short snippets, and very large files are slow in a tab.
- Expecting the regex heuristic to be perfect: a / in genuinely ambiguous code can be read as division or a regex the wrong way — diff the output.
Limitations
This is a tokenizer, not a full parser, and its limits are honest ones. TypeScript and JSX are not understood: type annotations, generics, and tags can still come out mangled, so use Prettier for those. The regex-vs-division decision is a heuristic — the same one syntax highlighters use — and adversarial code can fool it, for example a / after a } whose block-ness was guessed wrong. There is no full syntax validation: only unterminated strings, template literals, regex literals, and block comments are reported; other broken code is reformatted as-is. The formatter does not check that the output parses, it has no minify mode, and it does not preserve the original line breaks inside comments or templates beyond keeping their text verbatim. Very large files (tens of megabytes) can be slow in a browser tab, so format just the section you are debugging.
Structured Input/Output Example
function calculateSum(a,b){if(a===null||b===null){return 0;}let sum=a+b;console.log("Calculated sum:",sum);return sum;}const result=calculateSum(10,20);function calculateSum(a, b) {
if (a === null || b === null) {
return 0;
}
let sum = a + b;
console.log("Calculated sum:", sum);
return sum;
}
const result = calculateSum(10, 20);Related Tools
HTML Formatter
HTML formatter: turn minified or messy HTML into clean, indented markup. Nested tags aligned, attribute spacing normalized — view-source, readable.
CSS Beautifier
CSS beautifier: untangle minified stylesheets into readable CSS — one rule per line, properties indented, selectors aligned. Free, offline, private.
JWT Decoder Offline
JWT decoder offline: inspect a token’s header, payload claims, and expiry as readable JSON. 100% client-side — production tokens never leave your browser.