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.

1
  • Just stick with void 0 if you're worried about undefined being redefined somewhere. Commented Jul 5, 2018 at 17:18

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

OK - So Javascript allows us to use Javascript undefined as a variable name, hence the recommendation is to use 'undefined' explicitly ...
@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
but this throws error 'undefined' has already been declared
@HikmatGurbanli Just tried it in Node, I'm surprised to say that's correct. Older versions that was certainly not the case.
@hikmat then wrap it in a block.
|
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?

7 Comments

The first two are not the same. typeof returns string always
Yeah, my bad, copy pasted and forgot to remove typeof, edited.
This is technically incorrect, they are not equivalent based on the syntax of quotes. My answer explains why.
You should read my answer again, I'm not using quotes on my second statement.
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
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.