0

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.

Ole
  • 41,793
  • 59
  • 191
  • 359

3 Answers3

4

Yes there is a very obscure edgecase:

let undefined = "confuse me!";

that means that

 "confuse me!" === undefined

might be true and

typeof undefined === "undefined"

might be false, but if

 typeof arrayName[index] === "undefined" 

is true then its definetly not defined, however it might not be undefined :)

But as this is terrible, you don't really have to consider that.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • OK - So Javascript allows us to use Javascript `undefined` as a variable name, hence the recommendation is to use 'undefined' explicitly ... – Ole Jul 04 '18 at 20:45
  • 2
    @ole no, that means that `"confuse me!" === undefined` might be `true` and `typeof undefined === "undefined"` might be false, but if `typeof arrayName[index] === "undefined"` then its definetly `undefined` – Jonas Wilms Jul 04 '18 at 20:46
  • but this throws error `'undefined' has already been declared` – Hikmat G. Jul 04 '18 at 20:48
  • @HikmatGurbanli Just tried it in Node, I'm surprised to say that's correct. Older versions that was certainly not the case. – jhpratt Jul 04 '18 at 20:50
  • 1
    @hikmat then wrap it in a block. – Jonas Wilms Jul 04 '18 at 20:50
  • OK - I updated the question with some additional testing around your illustration, and it looks like it's better to stick with the typeof check ... since undefined could be redefined ... Like you are saying. – Ole Jul 04 '18 at 21:01
  • @ole yeah but it can't be globally redeclared, only in your own code, and as you don't do this (i hope so) you don't have to consider this edgecase and `sth === undefined` is probably always more readable, faster to type etc. – Jonas Wilms Jul 04 '18 at 21:03
  • Yeah I like the shorter version better for readability, and I'm using typescript, which I think also warns, so in my case it's fairly safe. – Ole Jul 04 '18 at 21:17
1
if(typeof arrayName[index] !== 'undefined')

is the same as

if(arrayName[index] !== undefined) // without quotes

But not as you stated:

IIUC this is the same as `arrayName[index] !== 'undefined'?

if(arrayName[index] !== 'undefined') // this is not correct

How dangerous is it in JavaScript, really, to assume undefined is not overwritten?

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • The first two are not the same. `typeof` returns string always – charlietfl Jul 04 '18 at 20:47
  • Yeah, my bad, copy pasted and forgot to remove `typeof`, edited. – Marcos Casagrande Jul 04 '18 at 20:48
  • This is technically incorrect, they are not equivalent based on the syntax of quotes. My answer explains why. – DrewT Jul 04 '18 at 21:02
  • 1
    You should read my answer again, I'm not using quotes on my second statement. – Marcos Casagrande Jul 04 '18 at 21:04
  • They are not equivalent. One is asking if an index's value is undefined. The other if it's data type is undefined. In my answer I showed how a variable value can be defined while it's type is undefined. – DrewT Jul 04 '18 at 21:06
  • 1
    you're not correct, they're equivalent. Tell me one case where `(typeof arrayName[index] !== 'undefined') !== (arrayName[index] !== undefined)`, you won't find unless the edge case describe by @Jonas, but you won't have that in a real world scneario – Marcos Casagrande Jul 04 '18 at 21:08
  • @drewT the one checks if it contains the string `"undefined"`, but i guess thats rather a typo, the OP probably meant `=== undefined` and then they are equivalent. – Jonas Wilms Jul 04 '18 at 21:09
1

Undefined is a global variable, and as such its value can be reassigned. So it is possible that the variable undefined could not be undefined. But typeof an undefined value will always return 'undefined', so it is technically safer. In practice, most people wouldn't reassign undefined, so either works.

Brendan Gannon
  • 2,632
  • 15
  • 22