Let's break this down a bit to make sense of it.
The error -
TypeError: Cannot read property 'ID' of undefined
is applicable when using both the==
(type conversion comparison) and the===
(strict comparison) because there seems to be an issue with$scope.$storage.dnlist[i].ID
. But due to the difference between the two comparisons the problem is being masked when using the==
(type conversion comparison).==
is a loosely equal to or non strict check, it will use type conversion to convert both values to the same type before checking if they are equal. In your scenario because$scope.$storage.dnList[i].ID
is returningundefined
.undefined
is considered a falsy value in JavaScript (converts to false when coerced) so when the==
comparison uses type conversion, in this instance,undefined
is being coerced (forced) to a0
as this is another falsy value. Now that both values are integers it can perform it's equality check===
on the other hand is a strict equality check meaning that no type conversions are made. It will simply check if both values are exactly equal in both type and value. Which in this case is no, one is aninteger
and one isundefined
and therefore your error is thrown.
More info on equality checks here and truthy and falsy values here.