JavaScript Formatter
Indent and format JavaScript while preserving line-break semantics.
- Runs locally in your browser
- No upload required
Try it
Supported formats
Privacy
Runs entirely in your browser. Your input is never uploaded, stored, or sent to a third party.
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
- Scan the script into tokens, distinguishing strings, templates, regexes and comments from code.
- Re-indent each original line from the tracked brace depth.
- Normalise only the whitespace between tokens; never touch token contents or line breaks.
Usage
- Paste or type JavaScript.
- Choose a 2- or 4-space indent.
- 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
{
"text": "function f(){if(a){return 1;}return 0;}",
"indentSize": 2
}Template literal preserved
{
"text": "const t = `a ${b} c`;",
"indentSize": 2
}Regex and comment preserved
{
"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
- ECMAScript Language Specification — Ecma International
- MDN Web Docs — JavaScript reference — MDN
Send feedback
Tell us if this tool helped, report a problem, or suggest a tool. Only the message you type is sent — never the input you used in the tool.
Was this tool useful?
Prefer to write to us directly? See the contact page.