[thelist] JS typeof operator limitations

Rory.Plaire at wahchang.com Rory.Plaire at wahchang.com
Fri Nov 30 14:01:29 CST 2001

+| what sort of detailed information are you looking to get?  
+| you may not be
+| aware that all objects in javascript can be interacted with 
+| in the same way.

If an object is an Array.

Specifically, the document.forms array.

Interestingly if you pass document.forms into a function, and
document.forms[0] into the same function later, you get different results
than expected.

For instance:

function checkFormSet(formSet)
	if(formSet.length > 0)

When you pass the array to the function above, you get the name of the last

When you pass a form from the forms array, you get the name of the last

If you try to check with the typeof operator, you get "object" both times.

The only way around this is to check the type property... for forms, it is
undefined (i.e. == null in IE), for elements, it is the type of the element.

This seems a little kludgy, but since it works, I'm thinking of going for
it, unless there is something more elegant.

<rory disposition="Evolt: protects you from the irks of quirky browsers."

More information about the thelist mailing list