The value of the length property of any array is the actual number of elements (more precisely, the largest existing index plus one).
If you try to access this index, it will always be undefined because it is outside the bounds of the array (this happens in the last iteration of your loop, because the condition is i<=variables ).
In JavaScript, indexes are processed from 0 to length - 1 .
Also, make sure your two arrays have the same number of elements.
source share