I have a function that counts the elements in an array or the properties and methods in an object, but it's intended for array element counting.
function count(array) {
  if (typeof array != "object" || !array) {
    alert("Bug: Object is of wrong type!")
    return 0;
  var i = 0;
  for (index in array) {
    if (array[index]) i++;
    alert("Bug: element is undefined!")
  return i;
First I want to see whether there is a simpler way of doing this. Partly because of that I have been experiencing problems after I have run this functions, error messages saying that "X is null or not an object", where X is either the object in question, when I have tried to access properties or methods of the object.

For example:
function propChange(prop, e) {
  if (whichKey() == enterKey) {
    for (index in props) {
      if (!props[index].value 
            && (props[index].id != "txtp"
            //|| count(validSn)
This function works as long as that line in the middle of the function is commented. But when I remove the double slashes so that the array validSn is counted, the script interpretter complains about props[index].focus being null or not an object. Yet validSn has nothing to do with props[index].focus, it's a completely different array! Why does the script interpretter behave like this? What's the actual problem? I really need to count that array but now I can't do it because I'm getting this error, which I don't understand why it's connected to the function call.

Even other functions using for (a in b) {...} have caused me problems, which I have found out just recently and hence stopped to use those functions. Moreover I'm running IE7.