A third-party widget is throwing an error on your site, and the stack trace points to line 1, column 48,213 of bundle.min.js. A tracking snippet is doing something you did not expect and you want to know what. An old project's only copy of a script is the minified one. In each case you need to read JavaScript that was never meant to be read by people.
This guide explains what minification does to JavaScript, how far a beautifier can take you, where source maps and browser tools fit in, and how to format your own code consistently so it never becomes this hard to read in the first place.
What minifiers do to JavaScript
A JavaScript minifier makes code smaller in several stages, from harmless to transformative:
- Whitespace and comments removed. All indentation, line breaks and comments disappear.
- Syntax shortened.
if (a) { b(); }might becomea&&b();truemight become!0; several statements get joined with commas. - Names mangled. Local variables and function parameters are renamed to single letters:
cartTotal(items, coupon)becomest(n,e). - Dead code removed and functions inlined by more advanced tools.
Here is a small minified function and its beautified form:
function t(n,e){let r=n.reduce((n,e)=>n+e.price*e.qty,0);return e&&e.percent&&(r-=r*e.percent/100),Math.round(1.2*r)}
function t(n, e) {
let r = n.reduce((n, e) => n + e.price * e.qty, 0);
return e && e.percent && (r -= r * e.percent / 100), Math.round(1.2 * r)
}
The beautified version is far easier to follow: you can see it sums price times quantity, applies a percentage discount, and adds 20%. But it is still not the original code. The names t, n, e and r carry no meaning, and the return a && b, c construction is the minifier's shorthand for an if statement. Beautifying restores layout, not names or intent.
Obfuscated code, which deliberately encodes strings and scrambles control flow, is harder still. Formatting it helps, but understanding it takes patient manual work.
Beautify JavaScript with our tool
The JavaScript Formatter uses js-beautify, running in your browser, so the code you paste is not uploaded.
- Open the JavaScript Formatter. Paste code into JavaScript input, or click Open file to load a
.js,.mjs,.cjs,.jsx,.tsor.jsonfile up to 20 MB. Load sample shows compressed cart helpers. - Choose the Indent size: 2 spaces, 4 spaces or Tab.
- Read the Formatted JavaScript. Each statement goes on its own line, blocks are indented, and spaces are added around operators. Blank lines that separate sections of the original are kept, reduced to one.
- To go the other way, turn on Basic minify. It removes comments, indentation and blank lines but keeps line breaks and never renames anything, so the code behaves exactly as before.
- Copy the result, or Download it as
script-formatted.jsorscript.min.js(or your file's own name).
Basic minify is useful for stripping comments from a snippet before pasting it into a CMS or tag manager. For real production minification with name mangling, use the JS Minifier or your build tool. The formatter does not check that the code is valid; if the output looks oddly indented, look for a syntax error near the point where the indentation goes wrong.
Better options when you can get them
Source maps
Production bundles are often published with a source map, a .map file that records how each position in the minified file corresponds to a position in the original source files. Look for a comment at the end of the script such as //# sourceMappingURL=bundle.min.js.map. When the map is available, browser developer tools automatically show the original files, with real names and comments, and stack traces point to the original lines. Nothing a beautifier does comes close.
If you publish code yourself, generate source maps in your build. You can keep them private (uploading them only to your error-tracking service) if you do not want the original source public.
Browser developer tools
In Chrome and Edge, open the script in the Sources panel and click the {} pretty-print button. Firefox's Debugger offers the same. You can then set breakpoints in the formatted view, step through the code and inspect real values at runtime, which is often the fastest way to understand what minified code does. Renaming variables mentally is much easier once you can see what they contain.
Prettier for your own code
For code you write, the standard answer is Prettier:
npx prettier --write "src/**/*.{js,jsx,ts,tsx}"
Prettier is opinionated: it rewraps long lines, normalises quotes and adds or removes semicolons according to its settings. That consistency is exactly what a team wants. js-beautify is gentler; it re-indents and spaces code but otherwise leaves your choices alone, which is better for quickly reading code you do not own.
Tips for reading unfamiliar code
- Start from the entry points. Search the formatted output for event listeners (
addEventListener), exported names, or string literals you can see on the page. Strings survive minification unchanged, so a visible error message is a great anchor. - Rename as you go. Paste the formatted code into your editor and use rename-symbol on single-letter variables as you work out what they hold.
- Run small pieces. Copy a self-contained function into the JavaScript Runner and call it with test values to confirm what it returns.
- Watch for regex literals. Minified code often contains dense regular expressions. Paste them into the Regex Tester to see what they match.
- Separate data from code. Large JSON blobs embedded in scripts are easier to read in the JSON Formatter.
A note on licences
Being able to read minified code does not grant rights to reuse it. Check the licence of third-party code before copying any part of it.
Frequently asked questions
Can I get the original variable names back from minified JavaScript?
Not from the minified file alone; the names were replaced and are gone. If the author published a source map, developer tools can show the original names and files.
Does beautifying JavaScript change how it runs?
No. A beautifier only changes whitespace and line breaks in ways that do not affect behaviour. Our Basic minify option also keeps behaviour unchanged, because it keeps line breaks and never renames anything.
What is the difference between minified and obfuscated code?
Minification shrinks code for faster downloads and makes it hard to read as a side effect. Obfuscation deliberately makes code hard to understand, for example by encoding strings and scrambling control flow.
Can the formatter handle TypeScript or JSX?
It accepts .ts and .jsx files and handles most common syntax, but it is designed for JavaScript. For TypeScript and JSX projects, Prettier understands the full syntax.
Why did the formatter not add missing semicolons?
A beautifier only changes layout. JavaScript's automatic semicolon insertion makes most omitted semicolons legal, and adding them is a style decision left to tools like Prettier or ESLint.