JavaScript Formatter

Indent and format JavaScript while preserving line-break semantics.

  • Runs locally in your browser
  • No upload required

Try it

Paste JavaScript. Line breaks are preserved so ASI semantics cannot change.

Formatted JavaScript
function f(){ if(a){ return 1; }return 0; }
Lines
5
function f(){
  if(a){
    return 1;
  }return 0;
}

Supported formats

Input
text, number
Output
text

Privacy

Runs entirely in your browser. Your input is never uploaded, stored, or sent to a third party.

Processing
In your browser
Privacy level
Browser only

Re-indented 1 line(s) of JavaScript at 2-space depth.

How it works

  • Tokenised the script, recognising strings, template literals, regular expressions and comments.
  • Tracked brace/parenthesis/bracket depth and re-indented each original line.
  • Collapsed redundant intra-line whitespace and preserved every original newline.
  • Existing line breaks are preserved, so `return` followed by a newline keeps its ASI meaning.
  • String, template, regex and comment contents are never altered.
  • Output is 5 line(s); formatting it again changes nothing.

Methodology

  1. Scan the script into tokens, distinguishing strings, templates, regexes and comments from code.
  2. Re-indent each original line from the tracked brace depth.
  3. Normalise only the whitespace between tokens; never touch token contents or line breaks.

Usage

  1. Paste or type JavaScript.
  2. Choose a 2- or 4-space indent.
  3. Run the formatter and copy the indented result.

Examples

These are examples, not current values: they use fixed inputs, while the interactive tool computes live results in your browser.

Function re-indented

jsonSelect to copy
{
  "text": "function f(){if(a){return 1;}return 0;}",
  "indentSize": 2
}

Template literal preserved

jsonSelect to copy
{
  "text": "const t = `a ${b} c`;",
  "indentSize": 2
}

Regex and comment preserved

jsonSelect to copy
{
  "text": "// match\nconst re = /a\\/b+/g;",
  "indentSize": 4
}

Limitations

Why line breaks matter

JavaScript inserts semicolons at line breaks in specific positions. A formatter that reflows freely can change behaviour invisibly; this one preserves the author's line breaks to keep semantics stable.

Frequently asked questions

Can formatting change what my script does?
No. Existing line breaks are preserved and string/template/regex/comment contents are untouched, so automatic semicolon insertion and token semantics are unchanged.
Why doesn't it split a one-line script?
Splitting is where semantics can break; the formatter deliberately preserves the author's line breaks.
Is my code uploaded?
No. The script is processed entirely in your browser.

Sources

  1. ECMAScript Language Specification — Ecma International
  2. MDN Web Docs — JavaScript reference — MDN