JavaScript Formatter

Format JavaScript, or minify and mangle it

Runs in your browserUtility01Formatters & Validators

What is JavaScript Formatter?

A JavaScript formatter re-prints your code using Prettier rules, with a choice of single or double quotes. Minify mode runs Terser for syntax-level compression and identifier mangling, and reports the size reduction. Everything runs locally in your browser, so your code is never uploaded.

How to use JavaScript Formatter

  1. 1Paste JavaScript or JSX code.
  2. 2In format mode, switch between single and double quote styles.
  3. 3Minify mode strips whitespace, collapses expressions, and mangles variable names in a single pass.
  4. 4Syntax errors are shown as the raw Terser or Prettier message so you can locate them.

How do I do this in code?

Use the tool above for one-off work; for anything you repeat, move it into a script or your project.

# Formatting: let Prettier own it, and enforce it in CI
npx prettier --write "src/**/*.{js,jsx,ts,tsx}"
npx prettier --check "src/**/*.{js,jsx,ts,tsx}"

# Minification: leave it to your build tool (esbuild / terser) instead of doing it by hand
npx terser input.js --compress --mangle -o output.min.js

Frequently asked questions

Can minified code be restored to its original form?+

Not completely. Minification discards variable names and comments, and formatting can only bring back the layout, never the lost semantic information. The right way to debug production issues is to generate source maps and keep them.

Why does minification report a syntax error?+

Usually the code contains syntax the current parser does not support, such as uncompiled TypeScript type annotations, decorators, or a very recent proposal. Compile it first, then minify.

Related tools

All tools