Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#12003 closed bug (invalid)

jQuery.inArray() returns -1 for value that is in array if it is the only element in array

Reported by: vic.emond@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocked by: Blocking:


var a = new Array(4);

$.inArray(4, a) returns -1.

This should return 0, shouldn't it?

Change History (4)

comment:1 Changed 9 years ago by scottgonzalez

Resolution: invalid
Status: newclosed

new Array(4) create [,,,] not [4].

comment:2 Changed 9 years ago by anonymous


var a = [4]; console.log($.inArray(4, a)); 0

var a = [1,4]; console.log($.inArray(4, a)); 1

comment:3 Changed 9 years ago by Rick Waldron

Yes, here you've used a literal. In your original example, you used the new Array() constructor, which creates arrays like...

var a = new Array(1);

a.length; // 1
a[0]; // undefined

var a = new Array(1, 2, 3);

a.length; // 3
a[0]; // 1

The Array constructor creates an array of N length when passed a single numeric argument

comment:4 Changed 9 years ago by dmethvin

alert(new Array(3).join("ha"));

Note: See TracTickets for help on using tickets.