Guide

How to Format JavaScript Text with Beautify or Minify

Enter JavaScript text, choose beautify or minify, set indentation when needed, and run the formatter. Empty input returns empty formatted text, while nonempty input falls back to 2-space indentation when its setting is missing or invalid.

Tool JavaScript Formatter

Choose the output style

JavaScript Formatter changes JavaScript text into either a more readable layout or a more compact text form. Beautify mode is useful when you need to inspect structure, compare sections, or review a compact function. Minify mode is useful when comments and eligible whitespace should be reduced. The result is a text transformation, not a report about whether the code is valid, compiled, executed, or semantically unchanged. Use a JavaScript text input and choose the output style that matches the task.

Format the JavaScript text

  1. Enter or paste the JavaScript text you want to format. Use text rather than another data type. A falsey non-string input has a special boundary: it can first be replaced by a truthy legacy JavaScript text value and then continue, while a non-string value normally fails instead of being formatted.
  2. Select beautify mode when readability is the priority. Leaving the mode missing or falsey also selects beautification. A string mode is compared after lowercasing: the string minify selects minification, while another string selects beautification. A truthy non-string mode can fail because it does not support lowercasing, so use a string mode or leave the setting unset.
  3. For nonempty input, set the indentation size used by beautification. Values from 0 through 8 are accepted. A missing, invalid, or out-of-range value is replaced with 2 spaces. The default is also 2 spaces, so an invalid setting does not produce an arbitrary indentation width.
  4. Run the formatter, then inspect the returned text. Empty or whitespace-only input is accepted and produces empty formatted text, which is different from a nonempty input that cannot be processed.
  5. Keep the input-size boundary in mind before processing a large file. Nonempty input larger than 1,000,000 UTF-8 bytes fails instead of being formatted. If an otherwise suitable request fails, the required JavaScript beautification dependency may be unavailable.

Read the returned text

In beautify mode, the selected indentation size controls the layout of the returned text. Existing line breaks can remain, with no more than two consecutive newlines preserved, and the formatter does not append a final newline. Parentheses are formatted without spaces placed inside them. These details can make the output differ from a manually styled version even when the source expresses the same intended structure.

In minify mode, line and block comments outside quoted strings are removed, eligible whitespace is collapsed, repeated blank lines are reduced, and surrounding whitespace is trimmed. Text inside quoted strings remains intact, including escaped characters. Do not assume that this processing covers every JavaScript syntax pattern or every comment and literal edge case.

Read the returned value as formatted text rather than as proof of correctness. A visually tidy result does not establish validity, compilation, execution, or semantic equivalence. Compare important output with the source and use a separate checking method when those properties matter.

Worked example

A developer has a compact JavaScript function and wants readable indentation before reviewing it.

Enter the function text, select beautify mode, set indentation to 4, and run the formatter.

The result is formatted JavaScript text containing line breaks and using the selected four-space indentation; no exact output value is assumed.

Limitations

  • Nonempty input above 1,000,000 UTF-8 bytes fails, and formatting can fail when the required JavaScript beautification dependency is unavailable.
  • Beautify and minify apply defined text transformations, so their handling may not cover every syntax, comment, or literal edge case.
  • Formatted output is a text result and does not by itself establish JavaScript validity, compilation, execution, or semantic equivalence.

Common errors

  • A truthy non-string mode can fail during mode selection because it cannot be lowercased. Pass a string such as beautify or minify, or leave the mode missing when beautification is intended.

FAQ

How do I beautify JavaScript?

Leave the mode missing or falsey for beautification, or provide a string other than minify. A truthy non-string mode can fail because lowercasing is unavailable for that value.

What indentation sizes are supported?

For nonempty text, choose an integer from 0 through 8. A missing, invalid, or out-of-range setting is replaced with 2 spaces, which is also the default indentation.

What happens if the input is empty or very large?

Whitespace-only text succeeds with empty formatted text. A nonempty input above 1,000,000 UTF-8 bytes fails rather than producing a formatted result.

Tool

JavaScript Formatter