JavaScript Formatter

Indent and space JavaScript so it can be read.

JavaScript Formatter tool

About the JavaScript Formatter

Reading a minified script, a bookmarklet or code pasted from a forum is much easier once it has proper structure. This JavaScript formatter prints code with consistent indentation, spacing around operators and one statement per line, and it recognizes strings, template literals, comments and regular expressions so their contents are never changed. Nesting becomes visible, which is usually enough to start understanding unfamiliar code or to spot a bug. Formatting cannot restore meaningful variable names that a minifier removed, but it makes the logic readable. For shrinking code again, use the JavaScript Minifier, and for testing snippets, the Live HTML Editor.

The difficult part of formatting JavaScript is telling code apart from everything that only looks like code. A curly bracket that appears inside quotation marks is just text, not the start of a block. A forward slash can begin a comment, divide two numbers or start a regular expression, depending on what precedes it, and template literals can hold expressions that contain further template literals. The formatter keeps track of all these cases, so strings, comments, regular expressions and template literals come out exactly as they went in while the code around them is rearranged.

It works on tokens rather than building a full syntax tree, which makes it quick and means it does not fail when it meets syntax it has never seen. The trade-off is that unusual hand formatting is evened out rather than kept. If your project has its own agreed style, a formatter configured in the build pipeline is still the better place to enforce it.

How to use the JavaScript Formatter

  1. Paste the codeMinified, bundled or written by hand, all of it can be formatted.
  2. Pick the indentationTwo spaces, four spaces or a tab.
  3. Run FormatThe neatly indented code appears below.
  4. Take the fileCopy it or download the formatted version.

JavaScript Formatter features

  • Even indentation for blocks, objects and arrays
  • Breathing room added around operators, after commas and next to keywords
  • Strings, template literals, comments and regular expressions kept unchanged
  • Arrow functions, classes and other modern syntax supported
  • One statement on each line
  • Keeps working when it meets unfamiliar syntax
  • Copy or download the formatted code

Common uses

  • Making sense of a minified or bundled script
  • Tidying code pasted from Stack Overflow, a chat message or an email
  • Getting a readable diff before reviewing a change
  • Seeing the overall structure of code you did not write
  • Fixing the indentation of a small script quickly

Reading JavaScript in practice: tracing an error in a minified widget, auditing third-party scripts, comparing versions and checking snippets

By the FreeToolsWorld editorial team · Updated November 2026 · How we write and check our guides

Formatted JavaScript is how developers read code they did not write: a minified widget throwing errors on their site, a third-party script they need to trust, a bookmarklet found online or a library that changed behaviour after an update. This guide traces an error in a minified script, then covers auditing what scripts send, reviewing bookmarklets, comparing two versions of a library, tidying copied snippets and when browser tools do the job better.

A worked example: an error in a minified widget

A shop's product pages show a console error, cannot read properties of undefined (reading 'price'), at line 1, column 48,213 of a reviews widget's minified script. With everything on one line, the location is meaningless. Formatting the script and searching for price leads to a function that reads product.price from a data object the page is supposed to provide. The shop's product template had stopped outputting that object after a theme update, so the fix belonged in the template, not the widget.

Browser tools first

Chrome, Edge and Firefox can pretty-print a script directly in the developer tools' Sources or Debugger panel, using a button marked with braces. Breakpoints then work on the formatted lines. A separate formatter is useful when you need to save, share or compare the readable code, or when examining a script outside the page that uses it.

Auditing what a script sends

Before adding a free widget, chat tool or tracking script to a site, it is worth knowing what it does with visitors' data. In formatted code, searching for fetch, XMLHttpRequest, sendBeacon and WebSocket finds where it sends information, and the addresses nearby show where it goes. Unexpected destinations or collection of form contents are reasons to choose a different provider.

Reviewing bookmarklets and console snippets

Bookmarklets and snippets pasted into the browser console run with full access to the page you are on, including logged-in accounts. Scammers exploit this by asking people to paste code into the console. Formatting a snippet before running it shows what it really does; if you cannot understand it, do not run it, especially on banking, email or social media pages.

Comparing two versions

When a library update changes behaviour and no changelog explains why, formatting the old and new minified files and comparing them with a text diff shows which functions changed. The variable names may be shortened, but the structure, strings and logic differences are usually enough to locate the change that matters.

Tidying copied snippets

Code copied from forums, documentation or AI assistants often arrives with mixed indentation and long lines. Formatting it before pasting into a project makes it consistent with surrounding code and easier to review. Read it properly afterwards as well, since formatting improves readability but says nothing about correctness or safety.

Semicolon surprises

JavaScript inserts missing semicolons automatically, but not always where people expect. A line ending without a semicolon followed by a line starting with a bracket or parenthesis joins the two into one expression, calling the previous value as a function. Reading formatted code, where statements are clearly separated, makes such joined lines easier to notice than in a dense original.

Project formatting

For files in your own project, a formatter integrated into the editor and run automatically before each commit keeps code consistent without anyone thinking about it. Agreeing a few settings, such as quote style, semicolons and line width, ends style debates in code review so attention goes to what the code does.

Learning from small libraries

Reading the formatted source of a small, well-written library is an excellent way to learn patterns: how it validates input, handles errors and exposes options. Start with libraries of a few hundred lines rather than large frameworks, and follow one feature from its public function down to the details.

Common mistakes

  • Running console snippets without understanding them.
  • Adding third-party scripts without checking what they send.
  • Debugging minified code line by column instead of formatting it.
  • Treating formatted code as reviewed code.

Related tools

To shrink scripts again, use JavaScript Minifier. For data files, try JSON Formatter, and to compare two formatted versions, Text Diff Checker.

Scripts you did not add

Hacked websites often carry injected scripts that are deliberately obfuscated, packed into long encoded strings and run with functions such as eval or atob. Formatting helps a little with reading them, but the more important signal is their presence: a heavily obfuscated script you did not add is a strong sign of a compromise that needs cleaning and a password reset.

Very large bundles

Formatting a multi-megabyte bundle in a browser tab can be slow. When you only need one part, search the minified file for a distinctive nearby string, such as an error message or an API path, copy a generous section around it and format just that section. It is usually enough to understand the code involved.

Privacy

Formatting runs in your browser. Your code is not uploaded or stored.

Frequently asked questions

Can formatting change how my code runs?

Only whitespace is changed, so behaviour stays the same. The one area to watch is code that leaves out semicolons and relies on the language inserting them; such code is fragile already, and it is worth adding the missing semicolons while you are tidying it.

Will the text inside my strings be changed?

No. Strings and regular expressions are recognised and copied exactly as written. That matters, because a bracket or slash inside quotes looks identical to one in real code, and a tool that mixed them up would corrupt the file.

Will it undo minification?

It brings back the indentation and line structure, but not the original names. A minifier that shortened variables to single letters threw that information away, and no formatter can work out what the names used to be.

Does it support TypeScript or JSX?

Plain TypeScript with type annotations generally comes out well. JSX embeds markup inside expressions, which a token-based formatter cannot fully understand, so for React components use the formatter configured in your project instead.

Should I use it as part of my build process?

That is not what it is for. In a project, set up a dedicated formatter once and let it run automatically; this page is meant for quickly reading and tidying code that comes from somewhere else.

Does my code leave my computer?

No. Formatting happens inside your browser, so proprietary scripts, client code and unreleased work never pass through a server or end up stored by a third party.