JavaScript Beautifier / Minifier

Beautify minified JS or minify it safely — strings, regex and semicolon-free code included

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

  1. Paste your JavaScript into the input area above.
  2. Pick an indent in the settings bar: 2 spaces (the default), 4 spaces, or tabs.
  3. Beautify with the button or Ctrl+Enter, or click Minify to compress instead.
  4. Copy the result with the Copy button or Ctrl+Shift+C.
  5. 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 its if, function or class. else, catch, finally and the while of a do…while join 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).
  • switch bodies are indented with each case one level in, and a braced case body like case 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 @license or @preserve are 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, .tsx and .jsx files.
  • A regex directly after ). Code such as if (ok) /x/.test(s) is supported. The tokenizer knows that a / after the ) of if, while, for or with starts 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.

Frequently Asked Questions

What is JavaScript beautification?

Beautification reformats JavaScript with consistent indentation, line breaks and spacing so a person can read it. It is most often used to unminify a production bundle: the beautifier puts each statement on its own line, indents every block, and spaces out operators. It changes layout only. Variable names that a minifier shortened stay shortened.

Does minifying JavaScript with this tool change how the code runs?

It shouldn't. The minifier only removes comments and whitespace, and it works on tokens, so strings, template literals and regular expressions are copied byte for byte. It also keeps every line break that automatic semicolon insertion could depend on. Code written without semicolons therefore still parses the same way. What it deliberately does not do is rename variables or rewrite expressions.

Why is my file bigger than the output from Terser or esbuild?

Production minifiers do far more than strip whitespace. They rename local variables to single letters, drop unreachable code, merge declarations and rewrite expressions, for example true becomes !0. On this site's own 59 tool scripts, whitespace and comment removal cut the size by 23.7 percent, while esbuild cut it by 38.3 percent. Use this tool for quick one-off jobs and a bundler for production builds.

Can I beautify TypeScript or JSX with this tool?

No. The tokenizer understands standard JavaScript up to ES2023, including classes, private fields, optional chaining, async functions, generators and template literals. TypeScript type annotations and JSX tags are not JavaScript syntax, so they may be spaced oddly or rejected. Use Prettier for TypeScript and React files.

Does the minifier keep license comments?

Yes. Block comments that start with /*! or that contain @license or @preserve are kept, because many open-source licenses require the notice to ship with the code. All other comments are removed.

Can a beautifier deobfuscate JavaScript?

Only partly. Beautifying restores the structure of the code, which is often enough to follow what a script does. It cannot bring back the original variable names or undo deliberate obfuscation such as string arrays and encoded identifiers. If you control the build, a source map is the reliable way to map minified code back to the original files.

Is my JavaScript code sent to a server?

No. The tokenizer, beautifier and minifier all run in your browser. Your code is never uploaded, which you can confirm in the Network tab of your browser's developer tools.