You open a production file and find one enormous line of short names and no spaces. It is valid JavaScript, but impossible to read. Formatting adds line breaks and indentation back, so you can follow the structure. The JavaScript Formatter does that in the browser, which is the first step in any investigation of a minified script.
JavaScript Formatter FREE online toolRuns in your browser. No sign-up, nothing to install. Open the tool →What formatting can and cannot do
Minifiers remove whitespace, shorten variable names and sometimes rewrite expressions. A formatter restores layout only. A function named a will still be called a, because the original name is gone unless you have a source map. Still, indentation alone makes control flow readable, and string literals, URLs and property names such as fetch or localStorage remain visible.
Format a script step by step
- Copy the minified code, or a portion of it if the file is very large.
- Paste it into the formatter and run it.
- Search the output for strings you care about, such as api, token, error messages or domain names.
- Follow the function that handles the event or request you are investigating, renaming variables in your notes as you learn what they do.
- Compare against a source map in your browser developer tools if the site provides one, since it restores the original names.
- Never run unknown code in a console on a logged-in page.
Typical uses
- Finding the endpoint a front-end calls when documentation is missing.
- Understanding why a third-party widget behaves strangely.
- Checking what data a tracking or advertising script collects.
- Locating the line behind an error reported as main.min.js:1:48213.
- Reviewing a suspicious snippet before deciding whether to block it.
Reading the error position
Browsers report minified errors by line and column, such as line 1 column 48213. After formatting, line numbers no longer match, so use the error message and nearby strings to find the spot. Better still, open the developer tools, enable source maps, and click the original location. If you control the build, generate source maps for your own production bundles and keep them private, so errors can be traced without making the code public.
Be careful with untrusted code
Reading code is safe; executing it is not. Do not paste unknown scripts into a live console or let them run on a page where you are signed in to important accounts. Obfuscated code, with strings built from character codes or heavy use of eval, can hide malicious behavior. Format it only to read, and if it looks suspicious, escalate to someone who analyzes such samples.
Respect licenses too. Reading a file to debug your own site is different from copying a proprietary library. Check the license header, which minifiers usually keep as a comment at the top.
Quick checks after formatting
Scan for obvious markers: calls to eval or new Function, long base64 strings, redirects through window.location, and requests to domains you do not recognize. None of these proves wrongdoing, since legitimate libraries use some of them, but each deserves a second look. Note the findings with the file name, version and domain so a colleague can reproduce what you saw.
Pick sensible settings
Use a consistent indentation, two or four spaces, and keep lines reasonably short so that nested callbacks remain visible. For very large bundles, format one module at a time. Copy the result into your editor, where folding and search help you navigate thousands of lines.
Comments (0)
No comments yet. Be the first to share a thought.