Format JavaScript

Format JavaScript with consistent indentation, spacing, and line breaks.

freeworks offlinenothing uploaded
ToolJavaScript Formatter
Input
Output
Put this on your own site

The frame below runs the same code as this page, in the reader's own browser. Nothing is sent to us, and nothing is sent to you.

Pick a dark background and the text and panels follow it, so the frame stays readable on a dark page.

Preview

How it works

Tokens are spaced and indented around braces, brackets, semicolons, and nesting while strings and comments remain opaque. Layout changes do not intentionally change program semantics.

  • Two-space indentation and 80-column wrapping are readable defaults.
  • Quote and semicolon styles remain configurable.

Worked example

Format minified JavaScript
Add indentation to compressed JavaScript code
Input
											function greet(name){if(name){console.log('Hello '+name);}else{console.log('Hello World');}}
										
Output
												function greet(name) {
  if(name) {
    console.log('Hello '+name);
  }
  else {
    console.log('Hello World');
  }
}
											

When to use this

Pre-commit hooks, code reviews, and readable generated bundles format JavaScript.

Edge cases

  • Template-literal braces are not block syntax.
  • Automatic semicolon insertion makes some line breaks significant.
  • Regex literals contain punctuation that is not an operator.

References