How to Beautify JavaScript: Reading Minified Code
Last updated: · 5 min read
Follow the stack trace of a production error and you usually land in a .min.js file: one line, tens of thousands of characters. The code works, but line breaks, indentation and meaningful variable names were thrown away by the build. The JavaScript Beautifier parses the code with Terser and prints it again with two-space indentation and one statement per line. This guide explains what formatting brings back, what it cannot bring back, and the small spelling changes the printer makes to literals, so that when you read the output you can tell what was in the original and what came from the tool. The outputs in the examples are the tool's real results.
Formula
Rules:
1. The code is parsed by Terser into a syntax tree; on a syntax error processing stops and the line and column are reported.
2. The tree is printed again with two-space indentation: one statement per line and a blank line between top-level statements.
3. Spaces are placed around operators, after commas and on both sides of =>; array literals are printed with spaces inside the brackets ([ 1, 2, 3 ]).
4. Missing semicolons are written out explicitly.
5. Comments are kept.
6. Compression and name mangling are switched off, so identifiers and logic do not change. Literals, however, are written the printer's way: the string 'y' becomes "y", the number 1000000 becomes 1e6, 0x10 becomes 16, and redundant parentheses are dropped.
Input (1):
function add(a,b){/* sum */const total=a+b;return total}const list=[1,2,3].map(x=>add(x,1));console.log(list);
Output:
function add(a, b) {
/* sum */ const total = a + b;
return total;
}
const list = [ 1, 2, 3 ].map(x => add(x, 1));
console.log(list);
Input (2):
const t=(e,n)=>e+n,r=[1,2].map(e=>t(e,1));
Output:
const t = (e, n) => e + n, r = [ 1, 2 ].map(e => t(e, 1));This is a parse-and-print operation, not a formula. The code is not edited as text: it is turned into a tree according to the ECMA-262 grammar and written out from scratch, so the spaces and line breaks of the input have no influence on the output. Code that relies on automatic semicolon insertion is parsed by those same rules, and the semicolons appear explicitly in the result. Only standard JavaScript is accepted.
What the inputs mean
- JavaScript
- The JavaScript code to format: a minified file or an untidy fragment. ES modules, classes and current syntax are supported; TypeScript and JSX are not. Up to 1,000,000 characters are processed.
Worked example: Expanding one-line code and looking at mangled names
- In the first input the body of add is opened up: its two statements go on separate lines with two spaces of indentation, a+b becomes a + b and the parameters are spaced as (a, b).
- The input has no semicolon after return total; the output completes it as return total;.
- The comment /* sum */ is kept and stays on the same line as the statement that follows it: /* sum */ const total = a + b;
- The three top-level statements are separated by blank lines. The array literal is printed as [ 1, 2, 3 ], with spaces inside the brackets; that is the Terser printer's style.
- The second input is code with mangled names. Output: const t = (e, n) => e + n, r = [ 1, 2 ].map(e => t(e, 1)); — spaces are added, but the names t, e, n and r stay as they are, and because it is a single declaration the code remains on one line.
Result: The first input becomes eight readable lines, with the function name, variable names and comment intact. In the second, only the spacing changes: const t = (e, n) => e + n, r = [ 1, 2 ].map(e => t(e, 1));
How to read the result
Formatting restores the structure of the code, not its names. Once a minifier has turned a variable such as total into t, the original name is gone from the file; the only thing that brings it back is the source map generated by the build. A source map links each position in the minified file to the original file, line and name, and the browser's developer tools show the original code when they find one. Without a map, the beautified output is still useful for following the control flow and setting breakpoints. Shorthand introduced by the minifier, such as !0 or void 0, also stays as it is. When examining a script you do not know, read it; do not run it. This is general information.
Common mistakes
- Expecting formatting to restore variable names. Mangled names such as t, e and n stay as they are; the original names need a source map.
- Pasting TypeScript or JSX. For const a:number=1; the tool reports "JavaScript syntax error (line 1, column 8): Missing initializer in const declaration".
- Expecting the output to match the original character for character. Single-quoted strings are printed with double quotes and 1000000 becomes 1e6; the meaning is the same, but a text diff will show differences.
- Deploying the beautified file. Indentation and line breaks make it larger; serve the minified version.
- Matching line numbers in beautified code against error traces. Positions in a stack trace refer to the minified file; after formatting, the lines have moved.
- Assuming comments always move to their own line. A block comment in front of a statement stays on the same line as that statement.
Frequently asked questions
Does beautifying change what the code does?
No. Compression and mangling are off; the code is parsed and printed again with the same statements. What changes is whitespace, line breaks, explicitly written semicolons and the spelling of literals (16 instead of 0x10, for example).
Can I recover the original variable names from minified code?
Only with a source map. Mangled names carry no trace of the originals. The source map format is specified in ECMA-426 and keeps the original identifiers in its names field.
Is code written without semicolons a problem?
No. The parser applies the automatic semicolon insertion rules of ECMA-262 and the output writes the semicolons explicitly, which is also a handy way to see how the code is really interpreted.
Why are there spaces in [ 1, 2, 3 ]?
That is how the Terser printer writes array literals; the tool has no option to turn it off. If your project needs a particular style, run the code through your own formatter afterwards.
Is my code uploaded to a server?
No. Parsing and printing happen in your browser; the code is not sent to a server.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.