JavaScript Formatter: beautify JavaScript online with Prettier

Input

Output

What does a JavaScript formatter do?

It rewrites the layout of your code (indentation, spacing, line breaks, quotes and semicolons) to one consistent style, without changing what the code does. It reads the code into a syntax tree first and prints that tree back out, so the result is laid out by the code's structure rather than by whatever spacing it arrived with.

This one is built on Prettier, the formatter most JavaScript projects already use, with Prettier's defaults. Code formatted here comes out the same as it would from npx prettier or a format-on-save in your editor.

Why use a JavaScript formatter?

  • Readability. Code copied from a chat message, an answer online or a bundled file is often crammed onto a few lines. Formatting it is the fastest way to see what it does.
  • Finding errors. A formatter has to parse the code before it can print it, so a missing bracket or an unterminated string is reported with its line and column.
  • Cleaner diffs. When everybody's code is formatted the same way, a code review shows what changed, not whose editor saved the file.

Example

Unformatted
const total=items.filter(i=>i.inStock).reduce((sum,{price,qty})=>sum+price*qty,0)
function greet(name){return 'Hello, '+name+'!'}
Formatted, default settings
const total = items
  .filter((i) => i.inStock)
  .reduce((sum, { price, qty }) => sum + price * qty, 0);
function greet(name) {
  return "Hello, " + name + "!";
}

With Quotes set to Single, Semicolons to Only where required and arrow parentheses to When needed, which is close to the “Standard” style many teams use:

Single quotes, no semicolons
const total = items
  .filter(i => i.inStock)
  .reduce((sum, { price, qty }) => sum + price * qty, 0)
function greet(name) {
  return 'Hello, ' + name + '!'
}

About Gradient Learning's JavaScript Formatter

It is free, needs no sign-up, and runs entirely in your browser: the code you paste is never uploaded, stored or logged, and it is never run, only read. The page keeps working if you go offline after it loads.

  • Modern JavaScript. Everything up to the current standard: async/await, optional chaining, classes with private fields, modules.
  • JSX. React components format as they would in a React project, with multi-line JSX wrapped in parentheses.
  • Not TypeScript. Type annotations are not JavaScript and are reported as a syntax error.

To run the code rather than format it, use the JavaScript Compiler.

How to use the tool

  1. Paste your JavaScript into the input panel on the left, or open a file.
  2. The formatted code appears in the output panel on the right as you type. There is nothing to click. If it cannot be parsed, a strip across the top of the output names the problem and its line and column; press the position to jump the cursor to it.
  3. Choose your indentation, quotes and semicolons, then use the copy or download button on the output panel.

Formatting options

Basic options (always visible)

OptionWhat it controlsPossible values
IndentationWidth of one indent level2 or 4 spaces, Tabs
QuotesQuote style for strings. A string containing more of the chosen quote keeps the other, to avoid escapesDouble, Single
SemicolonsWhether every statement ends with one, or only the lines that would otherwise run into the nextAlways, Only where required

Advanced options (under “More”)

OptionWhat it controlsPossible values
Line WidthLines longer than this are wrapped where the code allows40–200 (80 by default)
Trailing CommasA comma after the last item of a list split over several linesEverywhere, Objects and arrays only, None
Arrow Function ParenthesesWhether a single parameter is wrapped: (x) => x or x => xAlways, When needed
Spaces Inside Braces{ a: 1 } or {a: 1}On (default), Off

Frequently asked questions

Is the Gradient Learning JavaScript Formatter safe to use?

Yes. It runs entirely in your browser, so the code you paste is never uploaded, stored or logged, and it is never executed, only read. The page keeps working if you go offline after it loads.

Will formatting change what my code does?

No. Only layout changes: indentation, spacing, line breaks, quote style and semicolons. The formatter parses the code first and prints the same program back, and it only drops a semicolon where leaving it out cannot join two statements together.

Is this the same as Prettier?

Yes. It runs Prettier in your browser with Prettier's default settings, so the output matches what npx prettier or format-on-save in VS Code would give you. The options map one-to-one onto Prettier's own.

Why does it say “Unexpected token”?

The parser found something it did not expect at that position. The cause is usually just before it: a missing closing bracket or parenthesis, a missing comma between object properties or arguments, or an extra one. Pressing the line and column in the error puts the cursor there.

Does it support JSX and TypeScript?

JSX, yes: React components format as they would in a React project. TypeScript, no. Type annotations are not valid JavaScript and are reported as a syntax error.

Can it minify JavaScript?

No. Minifying JavaScript properly means renaming variables and removing unused code, which is a job for a build tool such as Terser or esbuild. Only stripping whitespace saves little and can break code that relies on a line break.

Is there a size limit?

Not on what you paste, but a file you open has to be under 1 MB. Formatting runs in a background thread, so a large file does not freeze the page while it works.