Skip to content

JavaScript Beautifier - Format JS Code Online

JavaScript input
Beautified JavaScript

Make JavaScript readable again

Production bundles, inline scripts and snippets copied out of dev tools tend to arrive as one long line. This beautifier (js-beautify) adds line breaks and indentation so you can follow the control flow. Paste code on the left and the formatted version appears on the right.

Options

  • Indent: two spaces, four spaces or tabs.
  • Braces: same line, same line but keep one-liners, own line (Allman), end expand, or keep as written.
  • Keep blank lines: keep blank lines from the input, collapsing runs of them to one.
  • End with newline: add a trailing newline, so Git does not report "No newline at end of file".

Quotes, semicolons, comments and names are never touched. If the indentation looks wrong towards the end of the output, the snippet is probably missing a closing brace or bracket. Multi-megabyte bundles can take a second or two, since the work happens on your machine.

Example

Input:

function total(items){let sum=0;for(const i of items){if(i.qty>0){sum+=i.price*i.qty}}return sum}

Output (2 spaces, same-line braces):

function total(items) {
  let sum = 0;
  for (const i of items) {
    if (i.qty > 0) {
      sum += i.price * i.qty
    }
  }
  return sum
}

Reading minified code

Beautifying a minified file gives you its structure back, but not its names. function(t,n) stays function(t, n) because the minifier threw the real names away. If the site ships a source map (look for a //# sourceMappingURL= comment at the end of the file), the browser's dev tools can show the original source, which beats anything a beautifier can do. Without one, search the formatted output for string literals such as API paths or error messages. They survive minification and are the quickest way to find the code you care about.

When to use something else

For your own project, run Prettier in your editor or as a pre-commit hook so every file stays consistent. This page is for one-off jobs: a script embedded in someone else's HTML, a tag manager snippet, or a bundle you want to read without opening an IDE.

Related tools

Going the other way? The JavaScript minifier shrinks code with Terser and shows the gzip size. To turn markup into React code, use HTML to JSX.

Related Tools

Frequently Asked Questions

Can this un-minify JavaScript?
It restores the layout: line breaks, indentation and spacing. It cannot bring back original variable names that a minifier shortened to single letters, or comments that were removed. For that you need the original source or a source map.
Does the beautifier change my quotes or semicolons?
No. Quotes, template literals and semicolons stay as written, and missing semicolons are not added. Only whitespace and line breaks change.
What do the brace styles mean?
Same line puts the opening brace at the end of the line, which is the usual JavaScript style. Own line (Allman) puts every brace on its own line. End expand keeps opening braces on the same line but moves else, catch and finally onto a new line after the closing brace. Keep one-liners leaves blocks such as { return x } alone if they were written on one line. Keep as written does not move braces at all.
Does it work with TypeScript or JSX?
Not well. The beautifier reads JavaScript tokens, so JSX tags get split up (<div> comes out as < div >) and TypeScript optional fields like b?: number come out as b ? : number. Type annotations on functions usually survive. For .ts and .tsx files, use Prettier.
Is my code uploaded?
No. It runs in your browser. Nothing you paste is sent anywhere.