JQuery inArray () returns -1 if the array contains one element

jQuery inArray returns -1 if the array contains one element.

var a = Array(1);
console.log($.inArray(1,a));

This returns -1. But if the array contains 2 or more elements, it works fine.

var a = Array(1,2,3);
console.log($.inArray(1,a));

Returns perfect position.

+4
source share
1 answer

Unlike what you think, it Array(1)does not create an array with an element 1, but an array of size 1. This is the specific behavior that you get when passing only one argument, and this is an integer.

From MDN :

, Array, 0 2 ^ 32-1 (), JavaScript , .

, Array, . :

var a = [1];
+6

Source: https://habr.com/ru/post/1539278/


All Articles