-1

Suppose I have someArray that at runtime will have either 0 or 1 elements.

Would this be a valid use of undefined?

var validItem = someArray[0];
if (validItem !== undefined) {
    validItem.doSomething();
}
Blackhawk
  • 5,984
  • 4
  • 27
  • 56
  • I mean, it works... did you test it? What do you consider "valid use"? This question is a bit vague. – Tyler Roper Apr 25 '19 at 14:05
  • Why do you test something that you say will always be true? – trincot Apr 25 '19 at 14:06
  • better use `(!! validItem)` for truthy/falsy – Void Spirit Apr 25 '19 at 14:06
  • 2
    You will get loads of answer for the same question [here](https://stackoverflow.com/questions/3390396/how-to-check-for-undefined-in-javascript) – Rahul Apr 25 '19 at 14:06
  • 1
    Your check will fail if you have this `someArray = [undefined, 1];` which is a perfectly valid Javascript array. – connexo Apr 25 '19 at 14:08
  • 1
    @AntihypeBird What if `validItem` is an empty string? Or the number `0`? Or the bool `false`? Catching specifically `undefined` vs catching *any falsy value* are very, very different. – Tyler Roper Apr 25 '19 at 14:08
  • I mean, it works, but am I using "undefined" behavior of JavaScript that will vary across implementations? It saves me a length check, but would it be comprehensible to another developer? Is it bad practice? – Blackhawk Apr 25 '19 at 14:09
  • @TylerRoper What if `someArray = new Array(1);`? This will result in an array with length 1 and look like this: `[ undefined ]`; – connexo Apr 25 '19 at 14:11
  • Not really. You're checking that the **first** key of validItem is not undefined. If you try and access the **second** you'd be SOL. Checking the _length_ of the array would be better in almost all cases. – Lewis Apr 25 '19 at 14:11
  • @connexo Fair enough. I suppose we could really go back and forth coming up with outliers for each and every solution. Without knowing the actual context around the question I don't think we'll be able to say "What is valid" to any certainty. – Tyler Roper Apr 25 '19 at 14:11
  • Why not just use `.length`? – Pointy Apr 25 '19 at 14:12
  • Let me change the scenario to make things a bit clearer... – Blackhawk Apr 25 '19 at 14:12
  • @Pointy See my previous comment on Tyler Roper's comments. – connexo Apr 25 '19 at 14:12
  • @connexo ooooh, that's nifty :D – Blackhawk Apr 25 '19 at 14:15
  • Given the feedback, should I just delete this and re-ask the question with some of these details already filled in? – Blackhawk Apr 25 '19 at 14:16
  • And perhaps I could include a minimal runnable example? – Blackhawk Apr 25 '19 at 14:18
  • 1
    I'd say it could be marginally useful as it was before your edit. – connexo Apr 25 '19 at 14:19
  • If you're really looking through an array of *objects* and using `.find()`, then I'd just use `if (validItem)`. It'll either be an object reference, and thus truthy, or it'll be nothing and thus falsy. – Pointy Apr 25 '19 at 14:20
  • 1
    @Pointy To keep this from getting messier than it already is, I reverted my edits so the question matches the answers. With what I've learned, I will ask a more precise question that includes ".find()" – Blackhawk Apr 25 '19 at 14:21

2 Answers2

1

this will throw you an exception if elements has no elements and you reference someArray[0]; it will throw you an error you should check like this

    if(someArray.length){
  someArray[0].doSomething();
}
onik
  • 1,579
  • 1
  • 11
  • 19
1

If your intention is to make sure you don't try to call a function on undefined, this is what I would do:

var validItem = someArray[0];
if ('doSomething' in validItem && typeof validItem.doSomething === 'function') {
    validItem.doSomething();
}
connexo
  • 53,704
  • 14
  • 91
  • 128
  • My intention is to avoid hitting someArray more than once - for the sake of argument, suppose someArray can change at any time. So if I do a "someArray.length > 0" test and then try to use an element "someArray[0].doSomething()", there are no guaruntees that it hasn't become empty since I checked. So instead, I was hoping to just "get" the first item in the list and then make the decision of whether it exists or not by checking against undefined before using it. – Blackhawk Apr 25 '19 at 15:27