JavaScript Formatter
Beautify or minify JavaScript code instantly in your browser. Handles regex literals, template literals, strings, and preserves licence comments during minification.
The JavaScript Formatter & Minifier is a free, browser-based tool for beautifying or compressing JavaScript code. All processing runs entirely in your browser — no code is ever sent to a server.
Features
| Feature | Detail |
|---|---|
| Beautify | Re-indents code, adds consistent spacing around operators and keywords |
| Minify | Strips comments and whitespace to produce the smallest possible output |
| Indent options | Choose 2 spaces, 4 spaces, or tabs |
| Keep semicolons | Control whether semicolons are retained in minified output |
| Regex-aware tokenizer | Correctly distinguishes /regex/ from division / to avoid corrupting code |
| String & template literal preservation | Single-quoted, double-quoted, and backtick strings are never modified |
| Comment handling | // and /* */ comments removed in Minify; /*! licence comments preserved |
| Load from file | Upload any .js, .mjs, .cjs, or .ts file |
| Stats | Shows original lines, output lines, token count, and file size |
| Copy & Download | One-click copy or save as .js or .min.js |
How to Use
- Paste your JavaScript into the input area, or click
Load fileto upload a file. You can also drag-and-drop a file onto the text area. - Select Output mode:
Beautifyto format, orMinifyto compress. - Choose an Indent style (2 spaces, 4 spaces, or tab — active in Beautify mode only).
- Toggle
Keep semicolonsto control whether semicolons appear in the output. - Click
Beautify JSorMinify JS. - Copy the result to clipboard or click
Downloadto save the file.
How Beautification Works
The tool uses a custom token-based formatter rather than executing or evaluating your code. The process has two steps:
Tokenizing
The source is scanned character-by-character into typed tokens: words, numbers, strings, template literals, regex literals, punctuation, operators, line comments, and block comments. A context-aware heuristic distinguishes regex literals (e.g. /\d+/g) from division operators (e.g. a / b) by examining what the previous meaningful token was.
Formatting
Tokens are reassembled with consistent rules:
- Opening
{stays on the same line as its statement; the body is indented one level - Closing
}dedents and appears on its own line - Binary operators (
===,&&,+, etc.) are surrounded by spaces - Unary operators (
!,~,++,--) are not padded - Keywords followed by
(— such asif,for,while— get a space before the parenthesis else,catch, andfinallyare placed on the same line as the closing}- Commas are followed by a single space
- Function calls and subscripts (
obj.prop,arr[i]) are compacted without extra spaces
Minification
In Minify mode all whitespace and comments are removed. Only the minimum spaces required to prevent tokens from merging (e.g. return value cannot become returnvalue) are inserted. The option Keep semicolons controls whether trailing semicolons before } are preserved. Disabling it produces slightly smaller output compatible with most modern environments.
/*! licence comments (e.g. from jQuery, Lodash, or Bootstrap) are always preserved in minified output.
Limitations
This tool is a token-level formatter — it does not parse a full Abstract Syntax Tree (AST). As a result:
- JSX / TSX syntax is not supported
- TypeScript type annotations may not always be formatted ideally
- Very complex operator chains may not be line-wrapped at optimal points
- Arrow functions with implicit returns across multiple lines may not be reformatted
For production codebases that need AST-accurate formatting, tools such as Prettier or ESLint with --fix are recommended. This tool is best suited for quick cleanup, inspecting minified code, and compressing scripts for deployment.
Frequently Asked Questions
Is my code sent to a server?
Never. The entire formatter runs in your browser using JavaScript. Your code is never uploaded or logged anywhere.
Can I format TypeScript with this tool?
TypeScript that uses only basic type annotations (: string, : number, interface, type) will generally format correctly because the tokenizer treats : as punctuation and type names as identifiers. Complex generic syntax or decorators may not format perfectly.
Does the tool support ES2022+ syntax?
Yes for most features — numeric separators (1_000), optional chaining (?.), nullish coalescing (??), logical assignment (&&=, ||=, ??=), BigInt literals (42n), and ** are all handled by the tokenizer.
What does "Keep semicolons" do in Minify mode?
When unchecked, trailing semicolons before } are removed (e.g. return x;} becomes return x}), saving a few bytes. This is safe for standard JavaScript but may cause issues in some edge cases — keep it checked if you are unsure.
Why does my regex look wrong after formatting?
The tool uses a heuristic to detect regex literals based on the preceding token. In rare cases — such as a regex immediately after a closing parenthesis — it may be interpreted as division. If this happens, try adding a comment or parentheses around the regex to clarify context.
What is the Tokens stat?
Tokens counts all non-whitespace tokens in the input — identifiers, operators, strings, numbers, punctuation, and comments. It gives a rough measure of code complexity.