You have this pattern all through the code, and with for loops it's actually not necessary.  If `i=0` and `length=0` then it will just skip the body of the for loop.  You could do `var length = keyElement && keyElement.length || 0` and you'd have an int or 0.