JavaScript Formatter/Minifier

Beautify or minify JavaScript - free, no signup.

JavaScript formatter / minifier
Mode

This is a lightweight, brace-based tool - it reformats and strips whitespace/comments safely (string contents are never touched), but it doesn't rename variables or restructure code the way a full build-tool minifier does.

Result

What this tool does

Paste your JavaScript and switch between Format (indents by brace nesting depth, keeping else/catch/finally on the same line as the preceding closing brace) and Minify (strips real comments and collapses insignificant whitespace). Both modes first split the code into strings, comments and everything else, so a string like "// not a comment" is never mistaken for an actual comment and never has its whitespace touched.

Formats JavaScript by indenting nested blocks according to brace depth, with else/catch/finally kept on the same line as the closing brace before them.

Minifies JavaScript by stripping real // and /* */ comments and collapsing whitespace that isn't needed to keep the code valid.

Correctly distinguishes real comments and whitespace from anything that merely looks like them inside a string or template literal.

When stripping a comment during minification would otherwise glue two identifiers together with nothing between them, inserts a single space instead of deleting straight through.

Forces a real line break after a stripped // line comment during formatting, so code that followed it on the next source line can never get pulled up onto the comment's old line.

How to use it

  1. Paste your JavaScript.
  2. Choose Format to beautify it, or Minify to compact it.
  3. Copy the result or download it as a file.

Why use this formatter

Readable JavaScript is easier to debug, while minified saves bytes. A lightweight formatter handles quick tasks without a build step.

Why we built it

We built the JavaScript formatter and minifier to leave the inside of strings and template literals alone, including ones containing // or /*, the classic bug of regex-based tools. Format the surrounding page with the HTML formatter and the CSS formatter.

Handy alongside this: the SQL Formatter/Minifier, the CSV Sorter and the XML Formatter & Minifier.

Who this tool is for

  • JavaScript developers cleaning snippets
  • Support staff reading minified code
  • Students

When to use it

  • Reading minified scripts.
  • Tidying a snippet.
  • Light minification of a small script.
  • Reviewing nesting.

The classic naive-minifier bug this avoids

A string containing // or /* is not a comment - but a careless minifier can't tell the difference.

A simple regex-based approach to stripping comments - searching the whole file for // or /* - will happily 'strip a comment' out of the middle of a string like const url = "https://example.com", corrupting the code by deleting everything from the // onward. This tool avoids that by first splitting the code into segments - strings, comments, and everything else - so a string's contents are always copied through completely untouched, and only genuine comment segments (found outside of any string) are ever removed.

Regex literals aren't tracked the way strings are

Strings and template literals are protected segments - a regex literal is not.

The tokenizer that powers this tool splits code into three kinds of segment: strings (single-quoted, double-quoted, or backtick template literals), comments, and everything else. Regex literals aren't one of those three - a pattern like /a\/\//g is just ordinary code text to the tokenizer, so if a regex contains a literal // or /* sequence, that sequence can be misread as the start of a comment and stripped or reformatted incorrectly. Plain strings never have this problem, because quote characters are unambiguous segment boundaries; a regex's slashes aren't tracked the same way. If your code has an unusual regex containing // or /*, check the output, or store the pattern in a string and build the RegExp with new RegExp(...) instead.

What you give it, and what you get back

Input and output of JavaScript Formatter/Minifier
ItemDetails
InputJavaScript.
ModeBeautify or minify.
OutputFormatted or compact code.

Issues you may run into

Common problems with JavaScript Formatter/Minifier
What you seeWhy it happensWhat to do
Variable names unchangedIt does not rename or tree-shake.Use a build tool for full minification.
Code behaves differentlyA rare edge case with regex or ASI.Test the output.
Modern syntax is unformattedIt is brace-based, not a full parser.Use a full formatter in your editor.

Assumptions and limitations

Frequently asked questions

How do I format JavaScript online for free?

Paste your code with Format mode selected, and it's reformatted with consistent indentation instantly.

How do I minify JavaScript online for free?

Paste your code with Minify mode selected - comments are stripped and unnecessary whitespace is removed immediately.

Does this protect regex literals the same way it protects strings?

No - only quoted strings and template literals are tracked as protected segments. A regex literal that contains a literal // or /* sequence (for example /a\/\//g) isn't recognized as a regex, so that sequence can be misread as a comment start. Put unusual patterns in a string and build the RegExp with new RegExp(...) if this matters for your code.

Will minifying ever merge two identifiers together?

No - if stripping a comment would otherwise leave two word characters (letters, digits, or underscores) touching with nothing between them, a single space is inserted in the comment's place instead of deleting straight through.

Will this break a string that contains // or /* characters?

No - the tool first identifies string and template literal boundaries, so their contents are always copied through untouched, and only genuine comments outside of any string are ever removed.

Does this rename variables or do advanced compression?

No - this is a lightweight formatting/whitespace tool, not a full build-tool minifier. It won't shorten variable names or restructure your logic.

Is my code uploaded anywhere?

No. Formatting and minifying happen entirely in your browser - nothing is sent to a server, and closing the tab clears it.