OK, let's first see what would happens if an array value not exist in JavaScript, so if we have an array like below:
const arr = [1, 2, 3, 4, 5];
and now we check if 6 is there at index 5 or not:
arr[5];
and we get
undefined
...
So that's basically give us the answer, the best way to check if undefined, so something like this:
if("undefined" === typeof arrayName[index]) {
//array value is not there...
}
It's better NOT doing this in this case:
if(!arrayName[index]) {
//Don't check like this..
}
Because imagine we have this array:
const arr = [0, 1, 2];
and we do:
if(!arr[0]) {
//This get passed, because in JavaScript 0 is falsy
}
So as you see, even 0 is there, it doesn't get recognised, there are few other things which can do the same and make you application buggy, so be careful, I list them all down:
undefined
''