JavaScript Formatter and Minifier

Paste JavaScript to beautify it with clean indentation, spacing and brace style, or apply a light minify that strips comments and indentation while keeping line breaks. It is free, instant and private in your browser.

JavaScript Formatter and MinifierPrivate · runs on your device
Input
Words 0Characters 0Lines 0
Formatted JavaScript

Example: function a(){return 1} → function a() { return 1 }

How to use the JavaScript formatter

Paste a script, choose a mode and copy the result. The output updates as you type.

  1. Paste JavaScript into the Input box, or press Paste. A minified bundle, a function copied from dev tools and a whole module all work, and Try it loads a short example.
  2. Set Mode to Beautify to re-indent the code, or to Safe minify (keeps line breaks) to remove comments and indentation.
  3. Choose the Indent: 2 spaces, 4 spaces or Tab. It only affects Beautify.
  4. Read the status line. It says "Beautified", or for Safe minify it shows the length before and after.
  5. Press Copy or Download .txt, then rename the download to .js. Use as input moves the result back into the input box.

Beautify uses the js-beautify library in your browser. Nothing is uploaded, and the tool never runs the code it formats, so pasting an unknown script here is safe even when running it would not be.

What Beautify produces

Beautify rebuilds indentation and spacing from the structure of the code. The example function a(){return 1} becomes three lines with the body indented. Each statement gets its own line, spaces are normalised around operators and commas, and the opening brace stays on the same line as the function, if or for that owns it. Object literals and destructuring patterns are spread over several lines, so const {a,b} = x is expanded rather than kept compact, while a short array of plain values such as [1, 2, 3] stays on one line.

Modern syntax is handled: arrow functions, classes with private fields, template literals, spread, async and await, optional chaining and modules. Comments are kept. Semicolons are neither added nor removed, and names are not restored, so a minified a or b stays as it is, though the control flow becomes readable. The tool does not check syntax: broken code is simply laid out as far as the beautifier can follow it, with no error message. JSX and TypeScript are not plain JavaScript and come out mangled, with spaces inserted inside tags and angle brackets.

Safe minify, line breaks and the cases to check

The mode is called safe because it keeps every line break. JavaScript inserts semicolons automatically at some line ends, so a return followed by a new line returns nothing, and a line that starts with a bracket continues the line above. Joining lines without a full parser could change behaviour, so this mode removes comments, trims each line, deletes blank lines and removes spaces that are not needed between tokens. The three-line function function add(a, b) with return a + b; inside comes out as function add(a,b){, return a+b; and }, and the status line reports 38 characters reduced to 32.

It is a character scanner, not a parser, so look closely at the output if your code contains:

  • Two plus or two minus signs separated by a space, as in a + +b or a - -b. The spaces are removed, giving a++b and a--b, which mean something different.
  • A regular expression literal after a keyword such as return. return /a +b/ loses its space and becomes a different pattern.
  • Multi-line template literals. The indentation of continuation lines is trimmed, changing the text.

For a production build use Terser, esbuild or SWC through your bundler. They parse the program, rename local variables, remove dead code and produce source maps. Use this mode for snippets and bookmarklets.

Common reasons to format JavaScript

Readable code is the starting point for every other task, and a formatter is the quickest way to get there.

  • Unminify a vendor script or bundle to find out where it posts data or why it throws.
  • Tidy code pasted from a chat message, PDF or forum answer that lost its indentation.
  • Normalise a file before comparing versions, so the diff checker shows logic changes rather than whitespace.
  • Read a bookmarklet or javascript: link before you run it.
  • Turn a long one-liner from the console into a function you can keep.

When the input is data rather than code, such as an API response or a configuration object, the JSON formatter also validates it, which this tool does not. If a pattern in your script misbehaves, the regex tester lets you debug it on sample text, and the HTML formatter will tidy the page that loads the script.

Quick answers

Frequently asked questions

The questions people ask most, answered in plain words.

Can I unminify JavaScript with this tool?

Yes. Beautify restores indentation, line breaks and spacing, so a minified bundle becomes code you can follow and debug. It cannot restore original variable or function names, because the minifier replaced them and they cannot be recovered. Only a source map, if one was published, can give the names back.

Why does Safe minify keep line breaks?

Because JavaScript inserts semicolons automatically at some line ends, and code written without semicolons depends on that. Joining lines can change what a return statement returns or merge two statements into one. Keeping the line breaks avoids those problems while still removing comments, indentation and extra spaces.

Will formatting change how my JavaScript runs?

Beautify only changes whitespace and line breaks, so ordinary code runs the same. Safe minify usually does too, but it is a simple scanner, so check code with spaced plus or minus signs, regular expressions after return, or multi-line template literals. Neither mode renames variables, reorders statements or runs your code.

Does the formatter support JSX or TypeScript?

No. Modules, classes, async functions, destructuring, optional chaining and template literals are fine, but JSX and TypeScript are not plain JavaScript. Tags and type annotations come out with odd spacing around angle brackets and colons. Compile them to JavaScript first, or use the formatter built into your editor.

Does it tell me if my JavaScript has a syntax error?

No. The beautifier lays out whatever it can follow and reports only that it finished, so broken code still produces output with no warning. To find syntax errors, run the code through your editor, a linter, or the browser console, which reports the line and the kind of error.