Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
    
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 5 hours ago
    
Possible duplicate of How to find the index of a missing value in an array? – djechlin 5 hours ago
    
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 5 hours ago
    
thanks for your replies!! now everything is clear! – JohnDoe 3 hours ago

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.

share|improve this answer
    
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 – Dustin Ryan-Roepsch 5 hours ago

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.