I have a big array which contains all kind of types (booleans, arrays, null, ...), and I am trying to access their propiety arr[i].length
, but some of them obiously fail to have length.
I wouldn't mind if the guys missing length returned undefined
(I could simply use arr[i].length||0
or something like that), but this is not the case, the whole thing crashes with some values (null
or undefined
for example).
var i, len, arr;
arr = [true, ["elm_0"], 99, "abc"]; //crashes if you add 'null' or 'undefined'
for(i = 0, len = arr.length ; i<len ; i++){
document.write(arr[i].length + "<br>");
}
document.write("I was executed");
- What other vars will crash besides
null
andundefined
? - How to prevent this from happening?