Common JavaScript Warnings

Mozilla's JavaScript Warnings provide developers and qa staff a valuable tool if they will use it. By striving to eliminate all JavaScript warnings from your code, you will automatically be able to locate common, hard to diagnose errors.

For more information about Mozilla's ability to log JavaScript warnings, please see Henrik Gemal's Using Mozilla in testing and debugging web sites.

JavaScript Warning: assignment to undeclared variable

This warning is one of the most common warnings and could be considered to be benign. Afterall, JavaScript does not require you to declare variables, so why should you care?

If you declare all of the variables in your script, this JavaScript warning will automatically point to any occurence where you might have mispelled a variable.

JavaScript Warning: redeclaration of function

This warning is a sign that you have multiple copies of a function in your page. This not only means that your web page is heavier than it needs to be since you are including redundant code but that you could have a situation where the same function is implemented differently in different scripts which can lead to inconsistent behavior in your site.

JavaScript Warning: redeclaration of var

This warning is a sign that you may have a variable name collision between different scripts which use the same variable name for different purposes.

JavaScript Warning: reference to undefined property

This warning is typically found in situations where the programmer is testing for the existence of an object in order to support differing DOM implementations.

For example,

if (document.all)
  elm = document.all[id];
else if (document.getElementById)
  elm = document.getElementById(id);

Will result in a "JavaScript Warning: reference to undefined property document.all".

While this warning is usually benign in cases of object detection, it can be a signal that you have mispelled a property reference. The frequency which this warning appears can also have the effect of hiding other more important warnings or convincing your developers to turn JavaScript warnings off.

Note: this warning will be modified in in Mozilla 1.8 to only appear in non-detecting uses of a property. For example,

var filters;

// detecting use (used as a test in a conditional)
// warning in Mozilla 1.7 but not 1.8
if (document.body.filters) 
{
  filters = document.body.filters;
} 

// detecting use
// warning in Mozilla 1.7 but not 1.8
filters = document.body.filters; 

// non-detecting use
// warning in both Mozilla 1.7 and 1.8
filters = document.body.filters[0]; 
JavaScript Warning: function does not always return a value

This warning appears when a function has more than one return statement but not all return statements return a value. This warning is a signal that in some circumstances, your function will return undefined as a value which may not be expected by the caller.

JavaScript Warning: variable hides argument

This warning appears when a function contains an argument and a declaration of a variable with the same name. This may be a problem if the variable is declared with an initial value which will override any value passed in the argument. For example:

function foo(bar)
 {
   var bar;
   alert(bar);
 }

will display an alert box containing the argument bar however

function foo(bar)
 {
   var bar = 'FOOBAR!';
   alert(bar);
 }

will display an alert box containing "FOOBAR!" regardless of the value passed in the argument bar.

home | up | topabout: