Well I can't follow the part with the length being 0, did you actually run the loop? It should work just fine.
But concerning the typeof operator, that operator (together with instanceof operator) is probably the biggest design flaw of JavaScript. It is near of being completely broken.
Although instanceof still has its limited uses, typeof really has only one
practical use case, which not happens to be checking the type of an object.
The JavaScript Typetable
Value Class Type
-------------------------------------
"foo" String string
new String("foo") String object
1.2 Number number
new Number(1.2) Number object
true Boolean boolean
new Boolean(true) Boolean object
new Date() Date object
new Error() Error object
[1,2,3] Array object
new Array(1, 2, 3) Array object
new Function("") Function function
/abc/g RegExp object (function in Nitro/V8)
new RegExp("meow") RegExp object (function in Nitro/V8)
{} Object object
new Object() Object object
In the above table Type refers to the value the typeof operator returns. As
you can see this is anything but consistent.
The Class refers to the value of the internal [[Class]] property of an object.
From the Specification: Class can be one of the following values:
"Arguments", "Array", "Boolean", "Date", "Error", "Function",
"JSON", "Math", "Number", "Object", "RegExp", "String"
In order to retrieve the value of Class one can has to make use of the
toString method of Object.
Checking the Class of an Object
function is(type, obj) {
return Object.prototype.toString.call(obj).slice(8, -1) === type;
}
is('String', 'test'); // true
is('String', new String('test')); // true
In the above code Object.prototype.toString gets called with
this being set to the object which its
Class value should be retrieved.
Checking whether a variable has been defined
typeof foo !== 'undefined'
The above will check whether foo was actually declared or not, since just
referencing it would result in a ReferenceError. This is the only thing
typeof is actually useful for.
To Sum it up
Don't use typeof unless you're checking for the existence of a variable.