JavaScript Minifier and Beautifier - Terser in Your Browser
JavaScript minifier and beautifier
Shortening local names saves the most bytes, but it breaks code that looks names up as text - inside eval, new Function, or a framework that reads parameter names. Turn it off if you rely on any of those.
What this tool does
Minifying JavaScript is not stripping spaces. Doing it by search-and-replace produces files that look fine and behave differently, and the difference shows up in production. This page runs Terser - the same engine behind the build step of most projects - inside your browser.
How to use it
- 1 Paste the JavaScript into the box.
- 2 Choose a target and decide whether to shorten names.
- 3 Press Minify - or Beautify to open a minified file back up.
Why a parser and not search-and-replace
JavaScript inserts semicolons where you left them out. Remove the newline between "return" and its value and the function starts returning undefined - the file is still valid, it just does something else. The same character means division in one place and a regular expression in another. Telling these apart needs the grammar, not a pattern, which is why this page uses a real parser.
What actually gets smaller
Whitespace and comments are the smallest part of it. The real savings come from renaming local variables to single letters, merging separate declarations into one, folding constants, and dropping code that can never run. Those are all tree operations - the minifier has to know that two names refer to the same binding, and that nothing between them reads it.
Where shortening names bites
A minifier can rename what it can see. It cannot see a name written as text: eval("count + 1"), new Function("a", "return a"), or a framework that reads a function's parameter names to decide what to inject. Those keep looking for the old name and find nothing. This is a property of the language, not a flaw in the tool - which is why the switch is yours to make rather than something applied silently.
Beautify is not the original file back
Beautify restores indentation and line breaks, so a minified file becomes readable again. It cannot restore what was thrown away: comments are gone, and if names were shortened they stay short - no tool can guess that "e" was once "errorMessage". For debugging a production file, a source map is the only thing that brings the original back.