12

I'm new at JavaScript and there is one thing that bothers me. I have got a very simple code:

var a = [];
a[1] = 1;

i = typeof(a[0]);
index = a.indexOf(undefined);
len = a.length;

console.log(a);
console.log("\n" + len);
console.log("\n" + i);
console.log("\n" + index);

My question is: Why indexOf returns -1, instead of 0. I know that this method compare by ===, but I used as a parameter keyword undefined. If I change method parameter to "undefined" it also doesn't work (but this for me it's obvious). Can someone explain me this and tell what is the simpliest way to find undefined value in array?

djechlin
  • 59,258
  • 35
  • 162
  • 290
JohnDoe
  • 121
  • 1
  • 4
  • `undefined` is a primitive in JavaScript, so it's it's own thing, what you have is an empty index that doesn't exist until you assign it something. So when `indexOf()` looks in an array for `undefined` it is actually looking for that value, if it returned a value for of every unpopulated index in an array it would go on forever – JmJ Mar 11 '16 at 21:08
  • Possible duplicate of [How to find the index of a missing value in an array?](http://stackoverflow.com/questions/30574147/how-to-find-the-index-of-a-missing-value-in-an-array) – djechlin Mar 11 '16 at 21:12
  • As noted above, `undefined` is a bit of a misnomer, it's actually a specific primitive value, so it is in fact defined, and not literally *not defined*, which is what you get when you have an array with unset indices. – adeneo Mar 11 '16 at 21:14
  • thanks for your replies!! now everything is clear! – JohnDoe Mar 11 '16 at 23:01

2 Answers2

13

This will in fact find an undefined value in an array, the problem is that your array a doesn't have any undefined values in it, so it returns -1 meaning it did not find any. Your array looks like:

[*uninitialized*, 1]

The fact that you put nothing in the first position doesn't mean it's populated with an undefined, it simply is not initialized/does not exist.

If you did something like:

var a = [undefined, 1];
var index = a.indexOf(undefined);
console.log(index);

It will in fact print 0 as expected.

Edit: to answer your question of how to find an uninitialized value, do the following

var a = [];
a[1] = 1;

for(var i = 0; i < a.length; i++){
    if(a[i] === undefined){
      console.log(i);
    }
}

This will print the index of uninitialized array values. The reason this actually works unlike indexOf is that a[i] will evaluate to undefined if:

(1) The element exists and it has the value undefined, or

(2) The element doesn't exist at all. indexOf however will skip these "gaps" in the array.

Pabs123
  • 3,385
  • 13
  • 29
  • I would add to this by saying if the goal of original poster is to try to find the first index that he has not yet assigned a value, he could use a loop and check in that manner – dustinroepsch Mar 11 '16 at 21:01
4

In general, arrays in JavaScript are sparse – they can have holes in them (That's why indexOf() returns -1 ), because an array is simply a map from indices to values. The array you are looking for is called dense it looks like

var a = Array.apply(null, Array(3)) 

or

var a = Array(undefined, undefined, undefined) 
a.indexOf(undefined) //0

Please have a look at this post, i hope it will help you

Community
  • 1
  • 1
The Reason
  • 7,705
  • 4
  • 24
  • 42