Format Code Now
/
Web & markupBabel JavaScript Formatter Next: TypeScript Formatter ›

Babel JavaScript Formatter

WM-89

Paste JavaScript or JSX and get consistently spaced, wrapped and indented code back. It parses with Babel through Prettier, in your browser.

Runs in your browser. Nothing is uploaded.

JavaScript or JSX

Formatted code

How to use it

  1. Paste your JavaScript or JSX, or open a file or drop it on the input box.
  2. Choose the indent, line width, semicolons and quote style.
  3. Press Format.
  4. Copy the result or download it as a .js file.

What the Babel parser accepts

Babel reads current JavaScript and JSX, including arrow functions, async and await, optional chaining, nullish coalescing, class fields, template literals, import and export, and decorators. That makes it a good fit for React code. Because it is the same parser many build tools use, code that formats here is usually code that your bundler can read too.

What changes and what does not

Only layout changes: indentation, line breaks, spacing, quote style and trailing semicolons follow your options. Names, values and the order of statements stay as they are, and comments are kept next to the code they describe. Long calls and objects wrap at the line width, and short ones are joined onto one line. A file with a syntax error is not partly formatted; you get the error with its line and column.

Limits

Nothing is run, linted or transpiled, so a variable that is never defined passes. Type annotations from TypeScript or Flow are not valid input here; the TypeScript formatter on this site handles those. Output follows the Prettier version in this page and can differ from the version in your own project.

Error messages and what to do

MessageWhat it meansFix
Could not format the code: Unexpected token at line 1, column 7.The parser found a character that cannot appear at that point, such as a missing name after const.Fix the code at the line and column shown, then format again.

Questions people ask

How do I format JSX online?

Paste it into the box and press Format. The Babel parser reads JSX natively, so tags, props and expressions in curly braces are laid out together. Long element lists are wrapped in parentheses at the line width.

Is this the same as Prettier?

It is Prettier, loaded in your browser with its Babel parser. You choose indent, width, semicolons and quotes. Other Prettier options and any .prettierrc file in your project are not read here.

Will it change what my code does?

Formatting only moves whitespace and, if you choose, quotes and semicolons. The tokens that matter to the engine stay the same. The one thing to watch is code that relies on automatic semicolon insertion, which is still correct after formatting.

Why do I get an error on valid TypeScript?

This page uses the Babel parser without type support. Type annotations, interfaces and generics are not accepted, so the parser stops at the first one. Use a TypeScript formatter for those files.

Can it format a whole project?

No. It formats one pasted file at a time. For many files, run Prettier from the command line with the same settings. Open one file, format it, and copy the result back; that works well for a few files but not for hundreds.

About this tool

Parses with Babel through Prettier, so it reads current JavaScript and JSX, including optional chaining, class fields, async functions and decorators. Format only changes layout: whitespace, line breaks, quotes and semicolons follow your options, and comments are kept. It does not run, lint or transpile the code, and it does not rename anything. Code that Babel cannot parse gives an error with the line and column. TypeScript and Flow type syntax is not accepted here; use the TypeScript formatter for that. Output follows the Prettier version in this page.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.