JavaScript Formatter
Beautify JavaScript and JSX with Prettier. Control indent, quotes, semicolons and more.
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
- Paste your JavaScript or JSX code into the left editor pane (or click Load Sample).
- Select your language: JS for plain JavaScript, JSX for React components.
- Choose your indent style: Tab, 2, 4, or 6 spaces.
- Set your preferred print width — Prettier will wrap long lines at this column.
- Toggle Semi, ' Quotes, { } Space, and Trailing Comma to match your team's style guide.
- Click Format to run Prettier. The formatted result appears in the right pane.
- Enable Live mode to auto-format as you type (400 ms debounce).
- Use Copy to copy the output, or Download to save as
formatted.js. - 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) andbabelwith 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
babelparser 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
- JavaScript Minifier — compress your JS for production with Terser
- TypeScript Formatter — format
.tsand.tsxfiles with Prettier - JSON Formatter — beautify and validate JSON data
- CSS Formatter — format and beautify CSS stylesheets
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.