Skip to main content
JF

JavaScript Formatter & Comment Stripper

Re-indent JavaScript based on brace depth, or strip comments and collapse blank lines for a lighter-weight file.

Did you like the tool? Thanks!
Share:


    

How to Use JavaScript Formatter & Comment Stripper

Paste JavaScript into the box. Format re-indents your code using brace-depth-based indentation (safely skipping over string, template-literal and comment contents). Minify strips // and /* */ comments and collapses blank/trailing whitespace. Both run locally in your browser — this is a lightweight readability tool, not a full parser.

About JavaScript Formatter & Comment Stripper

JavaScript is a full programming language, and that is precisely why a genuinely correct formatter or minifier for it is hard to build — tools like Prettier (formatting) and Terser (minification) work by parsing JavaScript into a complete abstract syntax tree (AST), understanding every language construct (arrow functions, destructuring, generators, optional chaining, template literals, regular expressions, and so on), and then regenerating code from that tree. Building a real JS parser is a multi-year engineering effort, not something to hand-roll for a browser widget, and this tool does not attempt it. Instead, this tool implements something much more modest and much safer: a brace-depth-based re-indenter for Format, and a comment-stripper plus whitespace-collapser for Minify — neither of which requires actually understanding JavaScript grammar, only correctly recognising where string literals, template literals, regular expressions and comments begin and end (so that a brace or a // inside a string doesn't get mistaken for real code). Format walks the source character by character, tracking nesting depth via {, [ and ( characters, and re-indents each line based on that depth — it does not rearrange, rewrite, or restructure your code in any way, it only adjusts leading whitespace. Minify strips // line comments and /* */ block comments, then removes blank lines and trims trailing whitespace from each line, without touching the code itself. The critical engineering detail in both modes is correctly skipping over content that merely *looks* like code but is not: a { character inside a string literal ("an object looks like { this }"), a // inside a template literal, or a /* inside a regular expression literal (/\/\*/ is a valid regex matching the literal text "/*") must never be treated as real syntax. This tool's tokenizer specifically tracks whether it is currently inside a single-quoted string, double-quoted string, template literal (including keeping template literal ${...} interpolation braces correctly balanced against the outer brace count), a line comment, a block comment, or plausibly a regex literal, and only counts braces/parses comments when none of those apply. Getting this wrong is the single most common way a naive "strip comments with a regex" script corrupts real-world JavaScript — removing what looks like a comment start inside a string, or miscounting brace depth because of an object literal string — so this is treated as the correctness-critical part of the implementation, not an afterthought. This is explicitly a lightweight, readability-focused formatter, not an AST-based tool, and it is honest about that limitation: it will not fix inconsistent code style beyond indentation (it will not add missing semicolons, normalize quote style, wrap long lines, or reformat multi-line expressions the way Prettier's line-width-aware algorithm does), and it will not catch or fix ASI (automatic semicolon insertion) hazards — JavaScript code that relies on subtle line-break-dependent parsing behaviour is passed through with its original line structure mostly intact, since this tool never merges or splits statements, only re-indents existing lines. For anything going into a real codebase, use Prettier (formatting) or ESLint (linting) — both understand JavaScript's actual grammar and will do a categorically better and safer job. This tool is aimed at quick readability fixes on a pasted snippet: unminifying a squashed one-liner enough to read it, or cleaning up copy-pasted code with inconsistent indentation, without needing to install anything.

Details & Tips

How Format works: the source is tokenized character-by-character while tracking whether the current position is inside a single-quoted string, double-quoted string, template literal, template-literal interpolation (${...}), line comment, block comment, or regex literal — braces/brackets/parens are only counted toward indentation depth when none of those states are active. Each output line is then re-indented with two spaces per unit of net depth at the start of that line (a line that both closes and reopens a brace, like '} else {', is indented at the depth before the closing brace). This only changes leading whitespace — it does not reflow, wrap, merge, or split any line of actual code. How Minify works: // line comments and /* */ block comments are removed (again, only when not inside a string/template-literal — a // inside a string is left alone), then each line has trailing whitespace trimmed and fully blank lines are dropped. Code is not otherwise compacted — statements are not merged onto fewer lines, variable names are not shortened, and whitespace within a line of real code is left as written, because doing any of that safely requires actually parsing the language. Why this is not a full minifier: a production-grade JS minifier (Terser, esbuild, swc) parses your code into an AST, then applies transformations like renaming local variables to single letters, removing dead code, merging statements with semicolons, and rewriting expressions into shorter equivalent forms — all of which require understanding scope, semantics and language grammar, not just tokenizing. Attempting shortcuts like this without a real parser risks silently breaking code (for example, mishandling automatic semicolon insertion when merging lines), which is an unacceptable risk for something that runs in production. This tool intentionally stays within what is safe to do with pure tokenization: comment removal and re-indentation. Known limitations: does not add or remove semicolons, does not detect or fix ASI hazards, does not reformat long lines or apply any line-width-aware wrapping, does not normalize string quote style, does not touch the contents of strings/template literals/regex literals under any circumstance (by design, for safety), and will not "fix" genuinely broken JavaScript — it operates on whatever tokens it finds without validating that the result is syntactically valid JavaScript, since it never builds a full parse tree. Recommendation: for formatting code that will be committed to a real project, use Prettier — it is free, extremely fast, and handles every JavaScript/TypeScript/JSX edge case correctly via a real parser. For production minification, use a bundler-integrated minifier such as Terser or esbuild's built-in minifier, which will produce meaningfully smaller and safer output than whitespace/comment stripping alone. This tool is best used for a quick, no-install readability pass on a pasted snippet.

Frequently Asked Questions

Is this a full JavaScript parser like Prettier?
No. It uses brace-depth tracking to re-indent code and simple tokenization to strip comments, without building a full abstract syntax tree. For production formatting, use Prettier, which parses JavaScript properly and handles every language construct.
Will Format touch the content of my strings or template literals?
No, the tokenizer specifically tracks string, template-literal and regex boundaries and never alters characters inside them, even if they contain braces or characters that look like comments.
Can this tool minify JavaScript for production?
Not meaningfully. Minify only strips comments and blank lines — it does not rename variables, remove dead code, or compact statements the way a real minifier like Terser or esbuild does. Use one of those for production builds.
Does Format fix missing semicolons or other syntax issues?
No, it only adjusts leading whitespace/indentation based on brace nesting depth. It does not add, remove, or validate semicolons, and does not check whether your code is syntactically valid.
Will stripping comments remove a // inside a string, like "http://example.com"?
No, the tokenizer recognises when it is inside a string or template literal and does not treat // or /* as comment markers in that context.
Does this tool handle regular expression literals correctly?
It attempts to recognise regex literals (e.g. /\/\*/ ) so that characters inside them are not mistaken for comments or braces, but regex detection in plain JavaScript is inherently ambiguous in some contexts, so unusual cases are not guaranteed to be perfect.
Will this reformat long lines or wrap them?
No, it never merges, splits or wraps lines of code — it only changes the leading indentation of existing lines. Line-width-aware wrapping requires a real parser like Prettier uses.
Is my JavaScript code uploaded to a server?
No, everything runs locally in your browser using plain JavaScript tokenization.
Can I use this on TypeScript or JSX?
It will generally re-indent based on braces reasonably well since TypeScript/JSX share JavaScript's brace/bracket structure, but type annotations and JSX syntax are not specifically understood, so results are best-effort rather than guaranteed correct.
What should I use instead for a real project?
Prettier for formatting (it understands the full JavaScript/TypeScript grammar) and a bundler-integrated minifier such as Terser or esbuild for production minification. This tool is meant for a quick, no-install readability pass on a pasted snippet.

Also Available As

javascript formatter, js formatter, js beautifier, format javascript online, strip js comments, js indent, javascript comment remover

Found a Problem?

Let us know if something with JavaScript Formatter & Comment Stripper isn't working as expected.

Thanks — we'll take a look.