process of removing all unnecessary characters from source code without changing its functionality
Minification (also minimisation or minimization) is the process of removing all unnecessary characters from the source code of interpreted programming languages or markup languages without changing its functionality. These unnecessary characters usually โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Minification_(programming)
Minification (programming) - Wikipedia
3 weeks ago - A source map is a file format that allows software tools for JavaScript to display different code to a user than the code actually executed by the computer. For example, to aid in debugging of minified code, by "mapping" this code to the original unminified source code instead.
๐ŸŒ
Minifier
minifier.org
Minify JS / CSS - JavaScript and CSS Minifier / Compressor
Minify JS and CSS using this online javascript / css compressor. You can paste .js or .css file, or just plain javascript or CSS code. This JS and CSS minifier removes whitespace, strips comments, combines files, and optimizes/shortens a few common programming patterns.
People also ask

What is JavaScript Minification?
Minification, or minimization, of JavaScript source code is the process removing all characters that aren't required for proper execution. These unnecessary characters usually include formatting characters, like: whitespaces, linebreak characters, comments, and in some cases block delimeters and end-of-line characters. After minification is applied, JS code is supposed to keep its functionality.
๐ŸŒ
minify-js.com
minify-js.com
Minify JS Online. JavaScript Minification tool that works in browser.
How does JavaScript Minification work?
Minification process is performed by a software or utility that analyzes and rewrites source code to reduce its size. Usually, minification process includes removal of whitespaces, shortening of variable names, and verbose functions replacement. Minification is performed on the server side and only when the source file is changed.
๐ŸŒ
minify-js.com
minify-js.com
Minify JS Online. JavaScript Minification tool that works in browser.
Why is Minification used?
Minification allows to reduce JavaScript file size that has a positive impact on load times and bandwidth usage. As a result, site speed and accessibility is higher compared to sites that don't use minification. Other words, minification tangibly improves user experience.
๐ŸŒ
minify-js.com
minify-js.com
Minify JS Online. JavaScript Minification tool that works in browser.
๐ŸŒ
Cloudflare
cloudflare.com โ€บ learning โ€บ performance โ€บ why-minify-javascript-code
Why minify JavaScript code? | Cloudflare
Minification, also known as ... and semicolons, along with the use of shorter variable names and functions. Minification of JavaScript code results in compact file size. For example, here is a block of code before and after minification:...
๐ŸŒ
Imperva
imperva.com โ€บ home โ€บ performance โ€บ minification
What is Minification | Why minify JS, HTML, CSS files | CDN Guide | Imperva
December 20, 2023 - The minified version of this sample code is 48% smaller. In some cases, minification can reduce file size by as much as 60%. For instance, thereโ€™s a 176 kb difference between the original and minified version of the JQuery JavaScript library.
๐ŸŒ
Minify JS
minify-js.com
Minify JS Online. JavaScript Minification tool that works in browser. | Minify JS Online
Also, we can see that Boolean "true" (which takes 8 bytes) was converted to "!0" (4 bytes). Finally, the "if" statement got replaced with a conditional operator statement. As a result, we get minified JavaScript code snippet that does the same job but takes 40% less physical memory when saved.
๐ŸŒ
Toptal
toptal.com โ€บ developers โ€บ javascript-minifier
JavaScript Minifier & Compressor | Toptalยฎ
JavaScript Minifier ยท ClearMinify ยท Copy to Clipboard ยท The API has changed, to see more please click here ยท To minify/compress your JavaScript, perform a POST request to ยท API https://www.toptal.com/developers/javascript-minifier/api/raw ยท with the input parameter set to the JavaScript you want to minify.See the documentation ยท
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-minify-minifying-js-with-a-minifier-or-jsmin
JavaScript Minify โ€“ Minifying JS with a Minifier or jsmin
November 2, 2022 - You don't have to do this minification process yourself. That's almost impossible. I'll share two minifying tools you can use. This tool removes whitespace, strips comments, combines files, and optimizes a few common programming patterns. You install the tool on your device and configure it in your code with the JavaScript path that you want you to minify for production.
Find elsewhere
Top answer
1 of 9
53

DIY Minification

No minifier can compress bad code properly.

In this example, I just want to show how much a minifier does.

What you should do before you minify

And regarding jQuery... I don't use jQuery. jQuery is for old browsers; it was made for compatibility reasons. Check Can I use; almost everything works in every browser (also Internet Explorer 10 is standardized now). I think now it's just here to slow down your web application... If you like the $(), you should create your own simple function. And why bother to compress your code if your clients need to download the 100 KB jQuery script every time? How big is your uncompressed code? 5-6 KB...? Not to talk about the tons of plugins you add to to make it easier.

Original Code

When you write a function you have an idea, start to write stuff and sometimes you end up with something like the following code.The code works.Now most people stop thinking and add this to a minifier and publish it.

function myFunction(myNumber){
    var myArray = new Array(myNumber);
    var myObject = new Object();
    var myArray2 = new Array();
    for(var myCounter = 0; myCounter < myArray.length; myCounter++){
        myArray2.push(myCounter);
        var myString = myCounter.toString()
        myObject[myString] = (myCounter + 1).toString();
    }
    var myContainer = new Array();
    myContainer[0] = myArray2;
    myContainer[1] = myObject;
    return myContainer;
}

Here is the minified code (I added the new lines):

Minified using (http://javascript-minifier.com/)

function myFunction(r){
 for(var n=new Array(r),t=new Object,e=new Array,a=0;a<n.length;a++){
  e.push(a);
  var o=a.toString();
  t[o]=(a+1).toString()
 }
 var i=new Array;
 return i[0]=e,i[1]=t,i
}

But are all those variables, ifs, loops, and definitions necessary?

Most of the time, NO!

  1. Remove unnecessary if,loop,var
  2. Keep a copy of your original code
  3. Use the minifier

OPTIONAL (increases the performance & shorter code)

  1. use shorthand operators
  2. use bitwise operators (don't use Math)
  3. use a,b,c... for your temp vars
  4. use the old syntax (while,for... not forEach)
  5. use the function arguments as placeholder (in some cases)
  6. remove unneccessary "{}","()",";",spaces,newlines
  7. Use the minifier

Now if a minifier can compress the code your doing it wrong.

No minifier can compress properly a bad code.

DIY

function myFunction(a,b,c){
 for(b=[],c={};a--;)b[a]=a,c[a]=a+1+'';
 return[b,c]
}

It does exactly the same thing as the codes above.

Performance

http://jsperf.com/diyminify

You always need to think what you need:

Before you say "No one would write code like the one below" go and check the first 10 questions in here ...

Here are some common examples I see every ten minutes.

Want a reusable condition

if(condition=='true'){
 var isTrue=true;
}else{
 var isTrue=false;
}
//same as
var isTrue=!!condition

Alert yes only if it exists

if(condition==true){
 var isTrue=true;
}else{
 var isTrue=false;
}
if(isTrue){
 alert('yes');
}
// The same as
!condition||alert('yes')
// If the condition is not true alert yes

Alert yes or no

if(condition==true){
 var isTrue=true;
}else{
 var isTrue=false;
}
if(isTrue){
 alert('yes');
}else{
 alert('no');
}
// The same as
alert(condition?'yes':'no')
// If the condition is true alert yes else no

Convert a number to a string or vice versa:

var a=10;
var b=a.toString();
var c=parseFloat(b)
// The same as
var a=10,b,c;
b=a+'';
c=b*1

// Shorter
var a=10;
a+='';// String
a*=1;// Number

Round a number

var a=10.3899845
var b=Math.round(a);
// The same as
var b=(a+.5)|0; // Numbers up to 10 decimal digits (32bit)

Floor a number

var a=10.3899845
var b=Math.floor(a);
// The same as
var b=a|0;//numbers up to 10 decimal digits (32bit)

switch case

switch(n)
{
case 1:
  alert('1');
  break;
case 2:
  alert('2');
  break;
default:
  alert('3');
}

// The same as
var a=[1,2];
alert(a[n-1]||3);

// The same as
var a={'1':1,'2':2};
alert(a[n]||3);

// Shorter
alert([1,2][n-1]||3);
// Or
alert([1,2][--n]||3);

try catch

if(a&&a[b]&&a[b][c]&&a[b][c][d]&&a[b][c][d][e]){
 console.log(a[b][c][d][e]);
}

// This is probably the only time you should use try catch
var x;
try{x=a.b.c.d.e}catch(e){}
!x||conole.log(x);

More if

if(a==1||a==3||a==5||a==8||a==9){
 console.log('yes')
}else{
 console.log('no');
}

console.log([1,3,5,8,9].indexOf(a)!=-1?'yes':'no');

But indexOf is slow. Read this: How do I check if an array includes a value in JavaScript?

Numbers

1000000000000
// The same as
1e12

var oneDayInMS=1000*60*60*24;
// The same as
var oneDayInMS=864e5;

var a=10;
a=1+a;
a=a*2;
// The same as
a=++a*2;

Some nice articles/sites I found about bitwise/shorthand:

http://mudcu.be/journal/2011/11/bitwise-gems-and-other-optimizations/

http://www.140byt.es/

http://www.jquery4u.com/javascript/shorthand-javascript-techniques/

There are also many jsperf sites showing the performance of shorthand & bitwise if you search with your favorite search engine.

I could go one for hours.. but I think it's enough for now.

If you have some questions, just ask.

And remember:

No minifier can compress properly bad code.

2 of 9
37

You could use one of the many available JavaScript minifiers.

  • YUI Compressor
  • Google closure compiler
  • Dean Edwards packer
  • JSMin
๐ŸŒ
JetBrains
jetbrains.com โ€บ help โ€บ webstorm โ€บ minifying-javascript.html
Minifying JavaScript | WebStorm Documentation
However, in the Project Tree, the file with the minified code is shown under the source JavaScript file which is displayed as a node. To change this default presentation, configure file nesting in the Project tool window Alt+1. The example below shows how you can use terser to minify your ...
๐ŸŒ
Google
developers.google.com โ€บ insights โ€บ minify resources (html, css, and javascript)
Minify Resources (HTML, CSS, and JavaScript) | PageSpeed Insights | Google for Developers
Use tools like HTMLMinifier for HTML, CSSNano or csso for CSS, and UglifyJS or Closure Compiler for JavaScript. A build process can automate minification, or the PageSpeed Module can optimize sites on Apache/Nginx servers.
๐ŸŒ
PageDart
pagedart.com โ€บ blog โ€บ how-to-minify-javascript
How to minify JavaScript - PageDart
September 26, 2025 - For example, JQuery (a popular javascript library) has a production version and a development version. The production version comes in at 19KB. The development version which is the same code is 120KB.
๐ŸŒ
Kinstaยฎ
kinsta.com โ€บ home โ€บ resource center โ€บ blog โ€บ javascript tutorials โ€บ how to minify javascript โ€” recommended tools and methods
How to minify JavaScript โ€” Recommended tools and methods
July 31, 2024 - To minify JavaScript code, you must parse it, compress it, and get the output. Once itโ€™s been minified, it should be almost unreadable to the naked eye. Youโ€™ve removed all the unnecessary white spaces, comments, newline characters, and everything that initially made the code legible. You may have to make some further changes to the code, too โ€” for example, inlining functions, removing block delimiters, using implicit conditionals, or rewriting local variables.
๐ŸŒ
Jscompress
jscompress.com
JSCompress - The JavaScript Compression Tool
JSCompress is an online JavaScript compressor that allows you to compress and minify all of your JS files by up to 80% of their original size. Copy and paste your code or you can upload and combine multiple files and then compress. We use UglifyJS 3 and babel-minify for all JavaScript minification ...
๐ŸŒ
DebugBear
debugbear.com โ€บ blog โ€บ minify-javascript-css
Minify JavaScript And CSS Code For A Faster Website | DebugBear
October 20, 2025 - But the CSS minification also resulted in some other changes: the two rulesets with the same selector were combined into one ยท the color: white rule was removed, since it was overridden by color: blue ... Finally, let's look at an example of minifying HTML. <!-- Web Dev Languages --> <ul class="list list--green" > <li>HTML</li> <li>CSS</li> <li>JavaScript</li> </ul>
๐ŸŒ
Elegant Themes
elegantthemes.com โ€บ blog โ€บ tips & tricks โ€บ how to minify your websiteโ€™s css, html & javascript
How to Minify Your Website's CSS, HTML & Javascript
January 25, 2023 - If you are looking for some offline tools to minify your HTML CSS or JavaScript locally, here are a few options: ... Paste in your source code or upload the source code file. Optimize the settings for a specific output (if options are available) Click a button to minify or compress the code. Copy the minified code output or download the minified code file. For this example, Iโ€™m going to use the minify tools from minifycode.com.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tools โ€บ minify
JavaScript Minify Tool | DigitalOcean
Minify your JavaScript source code with our interactive web interface for Terser.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ minify-js
Minify JS is JavaScript Minifier online
Online JavaScript Minify helps to Minify and Compress JavaScript data. It reduce the size of JavaScript and remove unwanted spaces.
๐ŸŒ
WPShout
wpshout.com โ€บ home โ€บ minify javascript
How to Minify JavaScript: A Step-by-Step Guide for Beginner Devs
July 10, 2023 - Iโ€™ve pasted the easy-to-read version of the code snippet into an online JavaScript minifier (see examples later). The minifier has generated another version of the exact same piece of code โ€“ except now the code is minified. This means unnecessary spaces and line breaks are removed to ensure the code is as small as possible.
Address ย  20 Povernei Street, 4th Floor, Flat no. 9, 010641, Bucharest
๐ŸŒ
WP Rocket
docs.wp-rocket.me โ€บ wp rocket knowledge base โ€บ features โ€บ minify javascript files and combine javascript files
Minify JavaScript files and Combine JavaScript files - WP Rocket Knowledge Base
Minified files contain a query string (?ver=) with the last modified timestamp, for cache busting purposes Original file: https://example.com/wp-content/themes/twentytwenty/assets/js/index.js?ver=1.5 Minified file: https://example.com/wp-content/cache/min/1/wp-content/themes/twentytwenty/assets/js/index.js?ver=1614629419 ยท Combined files use a random string in the filename each time it's regenerated, for cache busting purposes: https://example.com/wp-content/cache/min/1/ddaf06baadef88884af9a86038837e20.js ยท If only Minify is enabled, the order of the files on the page does not change, we just replace the existing JavaScript files with their optimized version.