About this minify JavaScript tutorial
This minify JavaScript tutorial explains every step on screen. First you see what a minifier deletes and why the browser does not miss it. Then the free Denwix JavaScript Minifier shrinks a real script, option by option.
After that the video moves to the command line and build tools, so the job runs on every release. It also covers WordPress plugins, VS Code, the PageSpeed warning, a real 420 KB example and the mistakes that break code.
Read the written guide: how to minify JavaScript, with every command and code sample.
Try it yourself in the free JavaScript Minifier. Your code never leaves your browser.
Minify JavaScript tutorial: full transcript
0:00 What this video covers
In this video you will learn how to minify JavaScript, step by step. We start with what minification really removes, then use a free online tool, the command line, build tools, WordPress and VS Code, and finally fix the PageSpeed warning.
0:15 What minification removes
Minification deletes the characters a browser does not need. Spaces, tabs, line breaks and comments help people read code, but the browser runs the same program without them, so a minifier throws them away.
A good minifier also shortens names. Inside a function, a long local variable like final amount can safely become a single letter, because nothing outside the function ever sees it. This trick is called name mangling.
It can also remove code that changes nothing. Here the constant called unused is never read, so the minifier drops it, and the function still prints exactly the same welcome message.
0:53 How much you save
How much do you save? A typical 120 kilobyte script drops to about 78 kilobytes after minification, and to around 23 kilobytes once the server adds Gzip on top. Your numbers will vary, but the pattern stays the same.
1:10 Online minifier: paste your code
Let us start with the quickest way, the free Denwix JavaScript Minifier. There is nothing to install, and your code never leaves the browser, because all the work happens on your own computer.
Paste your JavaScript into the box on the Paste code tab. Our sample has a licence comment at the top, a few normal comments, a console call and a debugger line, so you can see what each option does.
1:35 Choose the options
Remove comments is switched on by default. Keep licence banners stays on as well, so the copyright notice at the top survives, which many open source licences require.
For production code, turn on Drop console calls and Drop debugger. Log lines and debugger statements help while you build, but your visitors never need them.
Rename local variables shortens the names inside functions, and that is always safe. Rename top-level names stays off, because a global function may be called from another script or from your HTML. Tick it only when the file is a closed module.
Remove unused code, Fold constants and Compress syntax squeeze out the rest. Code that nothing calls is removed, simple sums are worked out in advance, and long syntax is rewritten in a shorter form.
2:25 Minify, copy and download
Now press Minify JavaScript, or press Control and Enter together. The minified result appears right below, packed onto one line and ready to use.
The status line shows the original size, the new size and the saving, plus an estimate of the Gzip size, so you know exactly how much your visitors will download.
Press Copy to put the code on your clipboard, or Download to save it as a ready minified file. Keep your original file as well, and from now on only edit the readable version.
2:56 Several files and Beautify mode
Have several scripts? Open the Upload files tab and drop in as many JavaScript files as you like. Each one is minified on its own, in your browser, with the same options.
The same tool also works the other way round. Switch to Beautify, choose two spaces, four spaces or tabs, and run it again. You get clean, indented code back from a minified file.
3:21 Terser and source maps
For real projects, automate the job. Terser is the most popular command line minifier. Install it with npm, then run npx terser with your input file, the output file, dash c to compress and dash m to shorten names.
Add the source map flag and Terser also writes a map file. Browser developer tools use it to show errors in your original code instead of one endless minified line, which makes debugging much easier.
3:50 esbuild, webpack and Vite
esbuild is extremely fast. Install it as a dev dependency and add the minify flag. Add the bundle flag as well, and it joins your whole app into one minified file in the dist folder.
With webpack you barely have to do anything. Set mode to production and run npx webpack, and the bundle is minified automatically with Terser. Add the Terser plugin only when you want finer control.
Vite minifies when you run npm run build. Its default minifier is esbuild, and you can switch to Terser with the build minify option in the Vite config file.
4:25 JavaScript and CSS in one step
To minify JavaScript and CSS in one step, install terser, postcss and cssnano, and give each one a build script. A single command, npm run build, then shrinks both. You can add an HTML minifier to the same chain.
4:41 WordPress plugins
On WordPress, a performance plugin such as WP Rocket, Autoptimize or LiteSpeed Cache minifies every theme and plugin script for you. Take a backup first, turn on one setting at a time, and test menus, forms, sliders and the cart after each change.
4:58 Minify in VS Code
In VS Code you have two choices. Install a minify extension and run it from the command palette, or open the built-in terminal and run Terser on your script, exactly as you would on the command line.
5:11 Fix the PageSpeed warning
Seeing the Minify JavaScript warning in PageSpeed Insights or Lighthouse? First, note which files it lists. Second, minify them and load the minified versions. Third, turn on Gzip or Brotli. Fourth, remove JavaScript you do not use, and then test again.
5:27 Real example: a 420 KB bundle
Here is a real world example. A small business site loaded one 420 kilobyte bundle. After minifying, removing unused libraries and adding Brotli and caching, the bundle fell to 245 kilobytes, mobile load time dropped to 3.1 seconds, and the Lighthouse score rose from 54 to 82.
5:48 Minify vs uglify vs obfuscate vs compress
Do not mix up the terms. Minify removes characters. Uglify is an older name for minifying with renaming. Obfuscate makes code hard to read on purpose, and compress, like Gzip, shrinks the file while it travels. Minification is not security.
6:04 Mistakes to avoid
Avoid the common mistakes. Fix syntax errors first, keep your source maps, never minify a file that is already minified, leave third party scripts as the vendor ships them, and never delete your original source.
6:18 How to de-minify JavaScript
To read a minified script, beautify it. Use our Beautify mode, the pretty print button in Chrome DevTools, or Prettier. The layout comes back, but the original names and comments do not, so always keep your source.
6:33 The workflow in short
So here is the whole workflow. Write readable code, minify a copy at build time, generate source maps, serve it with Brotli or Gzip and long caching, and test the live page after every change.
That is everything you need to minify JavaScript. Try the free Denwix JavaScript Minifier now, and read the full guide on our blog for every command and example.