Code: Avoid Optional Constructs and Kill Dead Code Fast

Chapter 8 - Ajax Optimization

In many situations, you can remove code and syntax constructs without harming the code. For example, given blocks for if statements and various loops that contain a single statement, you can remove the braces, so this:

if (likeJavaScript)
{
    alert("It's great!");
}

becomes this:

if (likeJavaScript)
alert("It's great");

You also may see that some statements, such as var, are not always needed. In JavaScript, variables spring into existence in the global space upon first use, so if you are using globals - which you shouldn't be, because they are a bad practice that increases the chances for variable namespace collisions from other included scripts - you can omit the var statement. When you do, this:

var global;
global = "domination";

would become simply this:

global = "domination";

As another byte-shaving example, you also can remove a return statement with no argument just before the end of a function. So, this:

function doWork( )
{
   /* complex code */
   return;
}

becomes this:

function doWork( )
{
    /* complex code */
}

You can employ other byte-shaving tricks to tune code, but generally you should let a tool such as w3compiler (http://www.w3compiler.com) do that for you.