What Is a JavaScript Beautifier?
A JavaScript beautifier takes code that is hard to read and reformats it with consistent indentation, line breaks and spacing. Usually that means minified code from a production bundle, a one-line snippet pasted from a chat, or a file that mixes tabs and spaces. Its counterpart, the minifier, goes the other way. It strips comments and whitespace so the file downloads faster.
This tool does both, and does them with a real tokenizer rather than search-and-replace. That matters because JavaScript has several places where a character means something different depending on context. A // inside a regular expression such as /https?:\/\// is not a comment. A ' inside a regex is not the start of a string. A line break between two statements in semicolon-free code is what keeps them two statements. A formatter that gets any of these wrong can silently corrupt working code.
How to Use the Tool
- Paste your JavaScript into the input area above.
- Pick an indent in the settings bar: 2 spaces (the default), 4 spaces, or tabs.
- Beautify with the button or
Ctrl+Enter, or click Minify to compress instead. - Copy the result with the Copy button or
Ctrl+Shift+C. - Clear the workspace with the Clear button or
Ctrl+L.
If the input cannot be tokenized, you get an error that names the line, such as Unterminated string on line 12, instead of half-formatted output.
What Beautify Does
The beautifier follows the conventions most JavaScript style guides share, and that Prettier and StandardJS output:
- K&R braces. The opening
{stays on the line of itsif,functionorclass.else,catch,finallyand thewhileof ado…whilejoin the closing brace. - One statement per line, with each nested block indented one level.
- Short objects stay inline.
{ retries: 3, timeout: 5e3 }stays on one line. Longer objects, or objects that already spanned several lines in your input, get one property per line. - Operators are spaced (
a = b + c,x ? y : z,(a) => a). Unary operators, member access and spread are not (!ok,obj.key,...args). switchbodies are indented with eachcaseone level in, and a braced case body likecase 2: {stays on the case line.- Comments and blank lines survive. Comments keep their position, and up to one blank line between statements is preserved.
For example, this minified input:
function debounce(fn,ms){let t;return function(...args){clearTimeout(t);t=setTimeout(()=>fn.apply(this,args),ms)}}
comes out as:
function debounce(fn, ms) {
let t;
return function (...args) {
clearTimeout(t);
t = setTimeout(() => fn.apply(this, args), ms)
}
}
Beautify does not add or remove semicolons, rename anything, or re-wrap long lines. The output is the same program with different whitespace.
What Minify Does — and What It Can’t Do
Minify removes comments, indentation and every space that isn’t needed to keep two tokens apart. A space between return and x is needed. A space between ) and { is not. Two kinds of content are left alone:
- Literals. Strings, template literals (including nested
${…}expressions) and regular expressions are copied exactly. - License comments.
/*! … */blocks and comments containing@licenseor@preserveare kept.
What it does not do is the heavy work a production minifier such as Terser, esbuild or SWC does. Those tools rename local variables, remove dead code and rewrite expressions. As a real-world measurement, the 59 tool scripts on this site total 577 KB. Whitespace and comment removal took them to 440 KB (−23.7%). esbuild’s full minification reached 356 KB (−38.3%). After gzip, which is how scripts actually travel, the savings were 15.4% and 23.3%. For production builds, let your bundler minify. Use this tool when you need to compress a single snippet or a bookmarklet without setting up a build step.
Semicolons, ASI and Why Naive Minifiers Break Code
JavaScript’s automatic semicolon insertion (ASI) treats some line breaks as statement terminators. In code written without semicolons, those line breaks are syntax. A minifier that simply deletes newlines would turn
const total = items.reduce((sum, n) => sum + n, 0)
const label = `Total: ${total}`
into const total=…,0)const label=…, which is a SyntaxError. This tool keeps a line break wherever the token before it could end a statement and the token after it could start one. So the output above stays on two lines:
const total=items.reduce((sum,n)=>sum+n,0)
const label=`Total: ${total}`
A line break after ,, {, ( or a binary operator, or before ., = or ,, can never end a statement, so it is removed. The same rule protects the restricted productions, where a line break changes meaning: return followed by a value on the next line, and ++/-- at the start of a line.
ASI also explains a classic bug that no formatter can fix for you. A line that starts with (, [ or a backtick does not get a semicolon inserted before it. It continues the previous expression. So const f = x => x followed by a line starting (function () {…})() actually calls x(...). Both modes preserve that meaning exactly, so if minified code misbehaves this way, the bug was already in the source. Standard-style code guards such lines with a leading ;.
Common Errors and What They Mean
- Unterminated string / template literal / comment. A quote, backtick or
/*was never closed, usually because the snippet was copied incompletely. The line number points to where the literal starts. - Unexpected “}” or “)”. The brackets don’t balance. This often means you copied the middle of a function. Beautify needs balanced brackets. Minify will still compress the fragment.
- Odd spacing around
<and>. That’s TypeScript generics or JSX, which this tool doesn’t parse. Use Prettier for.ts,.tsxand.jsxfiles. - A regex directly after
). Code such asif (ok) /x/.test(s)is supported. The tokenizer knows that a/after the)ofif,while,fororwithstarts a regex. After any other),/is read as division, which is what the language specifies.
Beautifier vs Prettier, ESLint and DevTools
Prettier is the standard for formatting source code you own. It parses a full syntax tree, wraps long lines at a configured width, and supports TypeScript, JSX, CSS and more. Run it in your editor or a pre-commit hook. ESLint finds bugs and enforces rules. Its stylistic rules overlap with formatting, but it is mainly a linter. Browser DevTools have a pretty-print button ({} in the Chrome and Firefox Sources panel) for reading a live script, and if the site ships source maps, DevTools shows the original files instead.
An online beautifier fills the gap between them: no install, no config, no project. Paste a minified snippet from a bug report, a third-party tag, or a Stack Overflow answer, and read it in seconds. For other formats, see the HTML beautifier, the CSS beautifier and the JSON formatter. To compare a file before and after a change, use the diff checker.