I need to see whether something there is an entry for an array index in Javascript and this answer say to uses (Essentially I changed it from === to !==):
if(typeof arrayName[index] !== 'undefined')
IIUC this is the same as `arrayName[index] !== 'undefined'?
I experimented with it and it works, but I want to make sure I'm not missing any edge cases?
Update
To clearify WRT to some answers given (Ran this on node 9.11.2
):
let undefined = "Hello";
console.log(undefined);
let arrayName = [];
if(arrayName[0] !== undefined) {
console.log("Test passes");
console.log("undefined is: ", undefined);
console.log("arrayName[0] is: ", arrayName[0]);
}
This prints:
Hello
Test passes
undefined is: Hello
arrayName[0] is: undefined
So it seems the answer is "No undefined could sometimes be redefind ..." ... and it's better to stick with typeof array[index] === 'undefined'
, but as some have indicated, undefined cannot be redefined globally, so it should be fairly safe to use the shorter version.