JavaScript Formatter
Format, beautify, and compress JavaScript instantly in your browser.
FreeBrowser-basedInstant
Result
Paste or type JavaScript on the left to see it formatted here.
Your files are processed locally in your browser and are not uploaded to our servers. Formatting happens instantly as you type, entirely in your browser — nothing is sent anywhere.
How to use JavaScript Formatter
Paste or type JavaScript into the input box and this tool reformats it with consistent brace-depth indentation as you type — a javascript format pass that keeps strings, template literals, and regex literals untouched. Switch to Minify to compress javascript into a whitespace- and comment-stripped version, then copy it to your clipboard or download it as a .js file. Everything runs locally as a js formatter online, with no code ever leaving your browser.
Key features
- Instant client-side formatting as you type
- Brace/paren-depth-aware indentation, including for-loop headers
- Adjustable indentation (2 spaces, 4 spaces, or a tab)
- One-click Minify mode that strips comments and collapses whitespace
- Correctly handles strings, template literals with ${} interpolation, and regex literals
- Regex-vs-division disambiguation using the standard token heuristic
- Syntax-highlighted result (keywords, strings, comments)
- One-click copy to clipboard, or download as a .js file
Practical use cases
- Cleaning up minified JavaScript pulled from a live site for readability
- Tidying up hand-written or auto-generated JS before a code review
- Compressing a script's whitespace and comments before shipping it
- Standardizing indentation across a file with mixed formatting
Supported formats
Accepted: javascript
Limitations
- A hand-written character-walking tokenizer, not a full AST parser like Babel or Acorn — it does not build a syntax tree
- Regex-vs-division detection uses a heuristic that covers the vast majority of real code but can rarely misjudge unusual constructs
- Minify mode strips comments and unneeded whitespace only — it does not rename variables, eliminate dead code, or perform the aggressive transforms a tool like Terser does
- Pretty mode reformats based on existing structure rather than making fresh line-breaking decisions, so it won't match Prettier's output line-for-line
- Very unusual or already-heavily-minified input, or deeply nested template literal interpolation, may not reformat perfectly
Frequently asked questions
- Does this tool upload or store my JavaScript anywhere?
- No. All formatting and minifying happens locally in your browser using JavaScript. Your source code is never transmitted or stored.
- Does Minify mode rename variables or remove dead code?
- No, and this is worth being explicit about. Minify mode only strips comments and collapses whitespace that is safe to remove — it does not rename variables, eliminate unused code, or restructure statements the way a full minifier like Terser or UglifyJS does. Think of it as a safe whitespace-and-comment compressor, not a complete build-tool minifier.
- How does it tell a regex literal apart from division?
- It uses the standard heuristic: a `/` starts a regex literal unless the previous meaningful token is an identifier, number, or a closing `)`, `]`, `}` — in which case it's treated as division — with an exception list of keywords (return, typeof, case, new, and similar) that are always followed by an expression. This works correctly for the vast majority of real-world code but, like any heuristic, can rarely be fooled by unusual constructs.
- Is the minified output safe to use in production?
- For most code, yes — the tokenizer carefully avoids touching the contents of strings, template literals, and regex literals, and keeps a newline or space anywhere removing it could change automatic semicolon insertion (ASI) behavior. That said, this is a hand-written tokenizer rather than a full parser, so you should sanity-check unusual or already-minified input before shipping it.
- Can it format code that uses template literals with ${} interpolation?
- Yes, including one level of nested template literals inside a `${}` expression. Deeper nesting is handled without crashing, but the formatting of very deeply nested interpolations is best-effort rather than guaranteed.
Related tools
Part of Coding Tools