Code Formatter · Tutorial

How to make minified JavaScript readable

4 min readbeginnerUpdated

Minified scripts squeeze a whole program onto one line, which makes them impossible to read. You will reformat one into normal, indented JavaScript and learn what formatting can and cannot recover.

What you need

  • The Code Formatter
  • A minified script, for example from your browser's developer tools or a .min.js file

Steps

  1. Paste the minified code

    Copy the script and paste it into the left box, or open the .min.js file. The example is a small FAQ accordion: one line, single-letter names and no spaces.

  2. Read the formatted version

    Each statement now sits on its own line and every function body is indented. You can see that the code finds the FAQ buttons and toggles the panel after each one when it is clicked.

    e.forEach(function (t) {
      t.addEventListener("click", function () {
        var n = t.nextElementSibling,
          o = "true" === t.getAttribute("aria-expanded");
  3. Make long lines easier to scan

    Lower Line width to 80 if expressions are still long. Prettier then breaks long calls and conditions across several lines.

  4. Rename as you go

    Copy the result into your editor and give the short variables meaningful names as you work out what they hold, for example t as button and n as panel.

Try it yourself

Opens the Code Formatter with this tutorial's sample loaded.

Try it now

Common mistakes

Expecting the original names back
Minifiers replace names and delete comments for good. Formatting restores layout, not meaning. If you own the code, look for a source map or the unminified source instead.
Pasting a whole HTML page
The formatter expects JavaScript only. Copy just the contents of the script tag, or use the HTML Formatter, which formats inline scripts too.
Editing the output box
The result is read-only. Press Use result as input if you want to keep working on the formatted code here.

Next steps

Go back to the Code Formatter, or keep going with one of these:

Tutorial · 4 min · beginner

How to format a React TSX component

Reformat a cramped React component written in TypeScript so its props, hooks and JSX are laid out cleanly and consistently.

FAQ

Is it safe to run the formatted code?

Formatting does not change behaviour, so it runs exactly as the minified version did. Only run code you trust.