2

Need to check if an array item at a specific index contains a value. The array index may or may not be defined, the array may or may not be defined. The value at the index could be number, string, or empty string - if index is defined at all.

is there anything wrong with

edit - removed quotes

if(undefined===array[0]){
    console.log('undefined');
}
4
  • You're sending in "undefined" as a string rather than testing against the value undefined. But you should be checking against typeof(array[0]) instead. So, what @Tomek said. Commented Jun 25, 2015 at 23:35
  • Do you care bout the difference between [] and [undefined]? Commented Jun 25, 2015 at 23:36
  • 2
    @Jan to be exact typeof variable not typeof(variable) as "typeof" is an operator not a function (I'm sure it was typo, but still, for later generations and so on ;) Commented Jun 25, 2015 at 23:39
  • "Won't someone think of the children!!" You are entirely corrent, @Tomek Commented Jun 25, 2015 at 23:51

2 Answers 2

3

Actually yes, you've mistaken "undefined" type with undefined value.

You can write either:

if (undefined === array[0]) {
    console.log('undefined');
}

or

if ('undefined' === typeof array[0]) {
    console.log('undefined');
}

And if array itself may be undefined, you should of course add the check for if before, e.g.:

if (undefined === array || undefined === array[0]{
    console.log('undefined');
}
Sign up to request clarification or add additional context in comments.

7 Comments

typeof returns 'string' if index is empty string.
Well yes, because then the value isn't undefined, is it?
I need to know if index contains a value - other than empty string or undefined.
Then you should also check if the string is empty.
checking if undefined seems to work for empty strings also.
|
0

@Tomek already has an excellent answer but I would still like to chime in here. One of your main problems is that you're mixing data types in your array, you shouldn't be doing that. If your array should contain strings and strings ONLY you can easily check it like this

if(typeof array[0] === "string" && array[0] !== "")

which would be a javascript equivalent of for example C#'s !string.IsNullOrEmpty.

Having multiple data types in the array complicates the checking and is indicative of an error in structuring your code. What happens for example if you enter null into the array? Then Tomek's test will fail. Of course, you could also do another check for null but I'd say it would be better to restructure your data model.

3 Comments

Data type is not defined by me. Could be the name of a part "carburetor", or could be part number '93672', or combination of letters and numbers, 'cats & dogs + birds'. Field is user defined. Using undefined first in the evaluation seems to cut out a few steps since i also have to check if array even exists.
Is the type of the value also user defined? As in, do they select from a set of predefined types (number, string)? If they do, then you could use that information to choose how to store the data. If they don't, then all you're really getting are strings and that's what you should be saving. I don't see the practical use of storing ids and names in the same array. If that's what you're doing and you don't fix the data model now I guarantee this will come back to bite you and be a recurring headache.
...the predefined types in a system that is liberal with data input shouldn't be number or string like I suggested earlier by the way, but actually what the data represents: part name, part number etc. And this would aid you in storing it. If you don't get that information, you'll have no idea what to do with the data anyways once you have it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.