Format and minify JavaScript code to improve readability or reduce file size.
JavaScript formatting (also called beautifying or pretty-printing) adds consistent indentation, line breaks, and spacing to make code readable. It is essential when working with minified third-party libraries, obfuscated code, or compressed API responses that need to be analyzed or debugged.
JavaScript minification does the opposite β it removes all unnecessary characters (whitespace, comments, newlines, optional semicolons) without changing the code's behavior. Minified JavaScript loads faster because it transfers fewer bytes over the network and parses more quickly in the browser. Most production websites serve minified JavaScript through a build tool like Webpack, Vite, or esbuild.
//) and multi-line (/* */) comments are removedconst by default, let when reassignment is needed, and never var in modern code===) instead of loose equality (==) to avoid type coercion surprisesFor production projects, use a dedicated build tool rather than manual minification. Vite uses esbuild for fast transpilation and Rollup for bundling with tree-shaking. Webpack with TerserPlugin provides aggressive minification with variable renaming. esbuild is the fastest standalone option, compressing JavaScript up to 100x faster than older tools. These tools also handle source maps, which map minified code back to the original source for debugging.
For typical JavaScript source code with comments and readable formatting, minification usually reduces file size by 30β60%. Combined with gzip or Brotli compression (applied by most web servers automatically), the transfer size can be reduced by 70β85% compared to the original uncompressed source. A 200KB source file might transfer as only 30β40KB.
Yes, using source maps. A source map is a separate .map file that maps minified code positions back to the original source. Build tools generate source maps automatically. Browser DevTools use source maps to show you the original, readable code even when the browser is executing the minified version.
This tool performs basic structural formatting β adding indentation and line breaks at brackets and semicolons. Prettier is a full AST-based formatter that parses your code into an abstract syntax tree and reprints it with consistent style according to configurable rules. For quick online inspection, this tool is sufficient. For project-wide formatting, use Prettier with an editor plugin.
You should never edit minified code directly. Always maintain human-readable source code and use a build pipeline to generate minified output automatically. The minified file is a deployment artifact, not a development file. Version-control your source; deploy your build output.