FIX Javascript “Cannot read property 'length' of undefined” when checking a variable's length