why is my indexOf statement creating an error

Go To StackoverFlow.com

2

I am trying to use .indexOf to find out if an array contains a string. Something is causing an error, but I don't know what. I'm sure this is a really simple thing, but I don't know the language and so don't see it. What's wrong?

//this code creates an error in IE
var myMines=new Array();
var newMine="22";
var myVar = myMines.indexOf(newMine);   //when I comment out this line, the page does not generate an error
2012-04-04 17:43
by bernie2436
older IE versions don't support indexOf for arrays - kirilloid 2012-04-04 17:45
also, what error are you getting - paul jerman 2012-04-04 17:46
Works for me in Chrome and Firefox - Tuan 2012-04-04 17:47
also, does not create an error in chrome. The value for myVar, in your case, is - - Guido Gautier 2012-04-04 17:47
Are you actually getting an error? Can you include that in the question - Rocket Hazmat 2012-04-04 17:50
the code doesnt generate any error on latest chrome - mpm 2012-04-04 17:50
P.S. The more "accepted" way to make an array is var myMines = []; - Rocket Hazmat 2012-04-04 17:58


3

Are you, by chance, using IE 8 (or lower)? Array.indexOf only works in IE 9+, Firefox 3+, and Chrome 7+.

Source: http://kangax.github.com/es5-compat-table/

Docs for Array.indexOf (also includes a function to implement this in browsers that don't support it): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf

2012-04-04 17:49
by Rocket Hazmat


3

Before being added in some modern browsers indexOf was exclusively a function used to search a string for another string and return the starting index. You are trying to use this function on an empty array and I'm guessing in a browser that does not support the indexOf function for arrays.

2012-04-04 17:47
by Kevin Bowersox
indexOf was added for arrays. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexO - Rocket Hazmat 2012-04-04 17:47
Using indexOf on an empty array will just return -1, not an error - Rocket Hazmat 2012-04-04 17:53


1

All you are doing it creating an array, and then seeing if something exists in it.

newMine is simply a value that you are attempting to look for inside an empty array. Try populating the array first.

2012-04-04 17:45
by Kevin
shouldn't it just return -1 though - paul jerman 2012-04-04 17:46
This is not the error. indexOf on a blank array will return -1, not an error - Rocket Hazmat 2012-04-04 17:47


1

What you do is basically:

  • creating an Array;
  • creating a variable;
  • forget to add it to the array;
  • asking an array for the position of an item that is nou in the array.
2012-04-04 17:47
by 11684
This would not return an error, it'd return -1 - Rocket Hazmat 2012-04-04 17:49


1

I've just posted your code into my Firebug console (running Firefox 11) and it worked. As your array is "empty" here, indexOf() just returns -1. Maybe you're script just can't handle a -1 value. However, I believe this code should work in most browsers. Who knows, maybe you've just found a bug in chrome's javascript implementation (^^).

2012-04-04 17:50
by Dominik M.
indexOf didn't work for arrays in IE <= 8 - Rocket Hazmat 2012-04-04 17:51


0

The IndexOf returns the current index/position (0 base) of a string, i.e.

var myMines="Hello World";
var myVar = myMines.indexOf('W');
alert(myVar); // Will alert 6 because 'W' is at the 7th position (first index is 0) so index is 6
2012-04-04 17:51
by The Alpha
indexOf was added for arrays. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexO - Rocket Hazmat 2012-04-04 17:51
Thanks there! A string is being converted to an array internally when indexOf being called and returns the index, as I know, Please make me clear if I'm wrong - The Alpha 2012-04-04 17:55
indexOf for strings and arrays have nothing to do with each other - Rocket Hazmat 2012-04-04 17:57
Does that mean same indexOf works in different ways for string and array - The Alpha 2012-04-04 17:58
I think so. I may be wrong too. It doesn't really matter though - Rocket Hazmat 2012-04-04 17:59
Thanks for your time and for the link - The Alpha 2012-04-04 18:00
You're welcome. I don't actually know how the internals of indexOf work, but it shouldn't matter to us JavaScript programmers - Rocket Hazmat 2012-04-04 18:01
Ads