Home/Dev Tools/JavaScript Formatter

JavaScript Formatter

Format

Beautify JavaScript and JSX with Prettier. Control indent, quotes, semicolons and more.

Language
Indent
Width
Style
Comma
Arrow
Mode
Input
Loading editor…
Output
Loading editor…
✨ AI Code Explanation
Mode

What Is a JavaScript Formatter?

A JavaScript formatter automatically rewrites your code with consistent indentation, spacing, quote style, and semicolons — without changing what the code does. It enforces a uniform style across a codebase so every developer's output looks the same, regardless of their editor settings. This tool uses Prettier, the industry-standard opinionated formatter trusted by millions of JS projects worldwide.

How to Format JavaScript Online

  1. Paste your JavaScript or JSX code into the left editor pane (or click Load Sample).
  2. Select your language: JS for plain JavaScript, JSX for React components.
  3. Choose your indent style: Tab, 2, 4, or 6 spaces.
  4. Set your preferred print width — Prettier will wrap long lines at this column.
  5. Toggle Semi, ' Quotes, { } Space, and Trailing Comma to match your team's style guide.
  6. Click Format to run Prettier. The formatted result appears in the right pane.
  7. Enable Live mode to auto-format as you type (400 ms debounce).
  8. Use Copy to copy the output, or Download to save as formatted.js.
  9. Click Explain with AI to get a plain-English explanation of what the code does.

Formatting Options Explained

  • Lang: JS / JSX — switches the Prettier parser between babel (JS) and babel with JSX enabled. Use JSX for React components.
  • Indent: Tab / 2 / 4 / 6 — indentation character and width per level. Tab is preferred by some teams for accessibility; 2 spaces is the most common in JS open-source projects.
  • Width: 80 / 100 / 120 — the column at which Prettier wraps long lines. 80 is the classic terminal width; 100–120 is common in modern IDEs.
  • Semi — when on, semicolons are added at the end of every statement. When off, Prettier omits them (Standard.js style).
  • ' Quotes — when on, string literals use single quotes. When off, double quotes are used. JSX attribute values always use double quotes regardless.
  • { } Space — when on, spaces are added inside object braces: { foo: bar }. When off: {foo: bar}.
  • Comma → ES5 — trailing commas in arrays and objects (safe for all modern JS environments).
  • Comma → All — trailing commas everywhere including function parameters (requires ES2017+).
  • Arrow: (x) / x — controls parentheses around single arrow function parameters. always: (x) => x. avoid: x => x.
  • Live — reformats automatically after a 400 ms pause while you type. Great for paste-and-review workflows.
  • No comments — strips all // single-line and /* */ block comments before formatting. Useful when preparing code for documentation.

JavaScript vs JSX — Which Should I Choose?

Choose JS for plain .js files: Node.js scripts, browser scripts, utility modules, or any file that doesn't contain JSX syntax. Choose JSX for React component files (.jsx) — this enables Prettier's JSX parser which correctly handles <Component /> syntax, self-closing tags, and JSX expressions. If you paste JSX into JS mode you'll get a parse error; switch to JSX and reformat.

Common JavaScript Formatting Errors

  • SyntaxError: Unexpected token — your code has a syntax error before formatting even runs. Fix the syntax (missing bracket, stray comma) and try again.
  • Unexpected < in JS mode — you're trying to format JSX. Switch Lang to JSX.
  • Output is identical to input — your code is already formatted to the current settings. Try changing indent or print width to see a difference.
  • Template literals look wrong — Prettier preserves content inside backtick strings as-is. This is intentional; it won't reformat string content.
  • import/export not supported — this tool uses the babel parser which supports ES modules, async/await, decorators, and all modern JS syntax.

Prettier vs Other JavaScript Formatters

Prettier is opinionated — it makes most style decisions for you, which is why it has become the dominant formatter in the JS ecosystem. ESLint is a linter that can also fix style but requires rule configuration. js-beautify (beautifier.io) offers more low-level control (brace style, comma-first) but produces less consistent output than Prettier. For team projects, Prettier + ESLint is the standard combination: Prettier handles formatting, ESLint handles code quality rules.

Related Tools

Is My Code Sent to a Server?

No — all formatting runs entirely in your browser using Prettier's standalone bundle. Your code never leaves your machine. No account required. No rate limits on formatting.