What is JavaScript formatting?
JavaScript formatting re-prints code with consistent layout: one statement per line, a semicolon at the end of each, spaces around operators and after commas, and blocks indented. This formatter uses Prettier, which parses the code first, so regular expressions, template literals, comments, JSX, and statements that end at a line break keep their meaning, and code it can't parse gets an error with the line and column instead of broken output. Minify uses Terser. Both run in your browser.
How the formatter lays out JavaScript
Prettier parses the code with Babel and prints it from the syntax tree: one statement per line, each ending in a semicolon, spaces around operators and after commas and colons, braces on the same line as if, else, function, and class, and every block indented by 2 or 4 spaces, as you choose.
Lines are kept within 80 characters where the code allows it: a long call, array, object, or chain of conditions breaks across lines, and a list that breaks gets a comma after its last item too. Comments stay where they were, and blank lines between statements are kept (several in a row become one). JSX is laid out like HTML: an element that contains other elements puts each child on its own line, wrapped in parentheses.
What formatting changes, and what it keeps
The program itself is kept: names, values, regular expressions, template literals, comments, and the order of statements. Statements that end at a line break end there in the output too, so return followed by a new line still returns nothing, and Prettier writes the semicolon that makes that visible.
Prettier does change how some things are written, in ways JavaScript treats as identical: strings use double quotes unless that needs more escapes, quotes are removed from object keys that don't need them, arrow function parameters get parentheses, numbers are written in lowercase without trailing zeros (.50 becomes 0.5 and 0XFF becomes 0xff), extra parentheses and empty statements are removed, a missing semicolon is added, and lists broken across lines end with a comma.
Errors with line and column
Code that can't be parsed produces no output. Instead the tool shows where the problem is, such as "Line 1, column 12: Unexpected token." for an object that is never closed, and marks it in the editor. Go to error moves the cursor there.
Format for editing, minify for production
Readable code is for the people who maintain it; minified code is for the browser. Minify uses Terser with its default settings: it removes comments (except license comments such as /*! … */) and whitespace, removes code that can never run and unused local variables, shortens expressions, and renames local variables and parameters to one or two letters. Names other code can see, such as global variables and exports, are kept. Format while you work, and minify as a build step before deploying.
How to format JavaScript
- 1Paste your code. Paste a function, module, or snippet into the input, or load the sample.
- 2Choose Format or Minify. Format re-indents the code with 2 or 4 spaces, as you choose, and the output updates as you type; Minify compresses it. If the code can't be parsed, the error shows the line and column.
- 3Copy the result. Copy the formatted or minified JavaScript into your project, or share a link to it.
Examples
Format compressed code
Input
function add(a,b){return a+b}const x=add(1,2);Output
function add(a, b) {
return a + b;
}
const x = add(1, 2);A statement that ends at a line break
return followed by a new line returns nothing, and the output shows it.
Input
function f(){return
42}Output
function f() {
return;
42;
}Regular expressions and template literals
The braces in the regular expression and the nested template literal are kept exactly.
Input
const re=/[{}]/g;const msg=`Total: ${items.map(i=>`${i.name} x${i.qty}`).join(", ")}`;Output
const re = /[{}]/g;
const msg = `Total: ${items.map((i) => `${i.name} x${i.qty}`).join(", ")}`;JSX
Input
const App=({title})=><div className="app"><h1>{title}</h1></div>;Output
const App = ({ title }) => (
<div className="app">
<h1>{title}</h1>
</div>
);4-space indentation
Input
if(ready){start()}else{wait()}Output
if (ready) {
start();
} else {
wait();
}An object that is never closed
No output is produced; the error is shown instead.
Input
const a = {Output
Line 1, column 12: Unexpected token.
Minify for production
The block becomes an expression and the parameters are renamed; sum keeps its name because other code can use it.
Input
const sum = (a, b) => {
return a + b;
};Output
const sum=(s,c)=>s+c;
Minify keeps license comments
Input
/*! MyLib v1.0 | MIT */
// helper
function double(value) {
return value * 2;
}
export { double };Output
/*! MyLib v1.0 | MIT */
function double(e){return 2*e}export{double};What the formatter changes
Layout always changes; these are the other changes, all ones JavaScript treats as identical.
| Before | After |
|---|---|
| const s='hi' | const s = "hi"; |
| const t='say "hi"' | const t = 'say "hi"'; (fewer escapes) |
| {'a':1,'b-c':2} | { a: 1, "b-c": 2 } |
| items.map(x=>x*2) | items.map((x) => x * 2); |
| .5 + 1.50 + 0XFF | 0.5 + 1.5 + 0xff |
| (a&&(b&&c)) | a && b && c |
| const x=1;; | const x = 1; |
Frequently asked questions
Does formatting change what my code does?
No. Prettier parses the code and prints the same program, so names, values, regular expressions, template literals, comments, and the order of statements are kept, and a statement that ends at a line break still ends there. Besides the layout, it only changes things JavaScript treats as identical, such as the quotes around strings and object keys, the case of hex numbers, and unneeded parentheses.
What happens if my code has a syntax error?
The tool shows where the problem is, for example "Line 1, column 12: Unexpected token." for an object that is never closed, marks it in the editor, and Go to error moves the cursor there. It doesn't produce output from code it can't parse, so you never copy broken code by accident.
What does minifying JavaScript do?
Minify uses Terser with its default settings. It removes comments (except license comments such as /*! … */) and whitespace, removes code that can never run and unused local variables, shortens expressions, and renames local variables and parameters. Global names and exports keep their names, so other scripts can still use them.
Does it support modern JavaScript?
Yes. Arrow functions, classes with private fields, async and await, optional chaining, template literals, destructuring, modules with import and export, and the rest of current JavaScript are formatted and minified.
Does it work with JSX or TypeScript?
Format handles JSX. Minify doesn't, because a browser can't run JSX until a build tool compiles it to JavaScript. TypeScript isn't supported: type annotations such as let x: number are syntax errors here, so use Prettier with its typescript parser for .ts files.
Can I choose the indentation?
Yes. Choose 2 or 4 spaces at the top of the tool; each nested block is indented by that much. Minify ignores it, because minified code has no indentation.
Which formatter and minifier does it use?
Prettier 3 with its Babel parser and a line width of 80 characters, and Terser 5 with its default settings. Both run in a background thread in your browser.
Is my code private?
Yes. Formatting and minifying run entirely in your browser. The only exception is the Share link button, which uploads the current output when you click it.
Is it free?
Yes — free with no sign-up or limits.