JavaScript Minifier - Minify JS Online with Terser
Minify JavaScript with Terser
Minifying strips whitespace and comments, shortens local names and rewrites expressions into shorter equivalents. This page runs Terser in your browser. Paste code and the minified output appears after a 300 ms pause.
See exactly how much you save
Under the editor are three sizes: original, minified, and minified plus gzip. The gzip number is the one to watch, because it is roughly what goes over the wire. For a 2 KB inline script the real saving may be a few hundred bytes, which is worth knowing before you give up readable code in a template.
Check the output before you ship it
Three things catch people out:
- Empty output. With ES module on, a file of top-level functions that nothing exports or calls is dead
code, and Terser removes all of it.
function helper(x){return x*2}minifies to nothing. Turn ES module off for classic scripts. - Code that reads names at runtime.
fn.name,constructor.nameand some dependency injection libraries see the mangled name. Turn off Mangle names if you rely on them. - TypeScript or JSX input. Terser stops with a syntax error at the first type annotation or tag.
Example
Input:
// Add tax to a price
export function withTax(price, rate = 0.2) {
const tax = price * rate;
return Math.round((price + tax) * 100) / 100;
} Output (mangle, compress, ES module):
export function withTax(t,n=.2){const o=t*n;return Math.round(100*(t+o))/100}
The comment is gone, the local names are one letter long, 0.2 became .2, and the exported name stays
the same so other modules can still import it.
Syntax errors with line and column
Terser has to parse your code before it can minify it. If the code has a syntax error, the tool shows the message with the line
and column, for example Line 3, column 14: Unexpected token, so you can jump straight to the problem.
Options
- Mangle names: shorten local variable, parameter and function names.
- Compress: apply safe code transforms such as dead-code removal and constant folding.
- Keep licence comments: keep
/*!,@licenseand@preservecomments. - ES module: allow
importandexportand treat the code as strict.
When to minify by hand
For a full project, let your bundler minify as part of the build. This tool is handy for small jobs: an inline script for a landing page, a bookmarklet, a snippet for a tag manager, or checking how much a function would cost in bytes. To read minified code instead, use the JavaScript beautifier.
Related Tools
Epoch / Unix Timestamp Converter
Convert between Unix epoch timestamps and human-readable dates.
Regex Tester
Test regular expressions against text with match highlighting.
UUID Generator
Generate random v4 UUIDs for use in databases and APIs.
Colour Converter
Convert colours between HEX, RGB, and HSL formats.