JavaScript Formatter & Minifier

Format uses js_beautify from js-beautify, which re-indents code without parsing it. Minify uses Terser, which parses the code, compresses it and can rename local variables.

0 characters

Original

0 chars

Output

0 chars

Change

-

Related Developer Tools

Runs in your browser: your files and text are not uploaded. Free, no account needed.

Last updated: · Reviewed by the QuickTooly Team

Two different engines

Format: js-beautify Without a Parser

js-beautify reads your code as a stream of tokens and adds indentation and line breaks. It does not check syntax, so it also formats code that contains errors. The tool sets the indent from the menu (default 2 spaces) and preserve_newlines: false, which removes blank lines. Comments are kept. Semicolons are not added or removed.

Formatting shows how semicolon-free code really parses

Automatic semicolon insertion (ASI) does not add a semicolon before a line that starts with ( or [. The formatter joins those lines, which makes the bug visible:

let x = y
(foo)()

is formatted as let x = y(foo)(), which is how JavaScript runs it. Add a semicolon after y if you meant two statements.

Minify: What Terser Does

Terser parses the code first. If the code has a syntax error, the output shows // Minify error: followed by Terser's message, for example Unexpected token: punc ((). The tool runs it with:

  • compress: true. Terser rewrites code into shorter forms and drops unreachable code.
  • "Mangle variable names" (on by default). Renames function parameters and local variables to single letters. Top-level function and variable names are not renamed, so other scripts can still call them.
  • "Remove comments" (on by default). Removes every comment. When unchecked, Terser keeps only comments that start with /*! or contain @license or @preserve. Ordinary // comments are still removed.

Worked example

The built-in sample (203 characters) minifies with both options on to 114 characters:

function calculateTotal(t){let e=0;for(let l=0;l<t.length;l++)e+=t[l].price*t[l].quantity;return e>100&&(e*=.9),e}

Terser has turned the if block into e>100&&(e*=.9) and 0.9 into .9.

When Mangling Can Break Code

  • Code that reads its own function or parameter names, for example through Function.prototype.toString or a dependency-injection framework that uses parameter names.
  • Code that uses eval or with, which can refer to local names Terser cannot see.
  • Debugging: mangled names make stack traces hard to read. No source map is produced.

JavaScript Formatter Questions

Can it format TypeScript or JSX?

No. Format treats JSX tags as comparison operators and spreads them out, for example < div className = "x" >. Minify stops with a syntax error on type annotations and JSX, because Terser only accepts JavaScript.

Does it handle import and export statements?

Yes. Terser accepts ES module syntax and keeps import and export statements in the minified output.

Why is the minified output still readable?

With "Mangle variable names" off, Terser keeps the original names and only removes whitespace and comments and applies compression.

Related Formatters