jQuery inArray how to use it right

0 votes

First time I work with jQuery.inArray() and it acts kinda strange.

If the object is in the array, it will return 0, but 0 is false in Javascript. So the following will output: "is NOT in array"

var myarray = [];
myarray.push("test");

if(jQuery.inArray("test", myarray)) {
    console.log("is in array");
} else {
    console.log("is NOT in array");
}

I will have to change the if statement to:

if(jQuery.inArray("test", myarray)==0)

But this makes the code unreadable. Especially for someone who doesn't know this function. They will expect that jQuery.inArray("test", myarray) gives true when "test" is in the array.

So my question is, why is it done this way? I realy dislike this. But there must be a good reason to do it like that.

May 31, 2022 in JQuery by Edureka
• 13,670 points
335 views

1 answer to this question.

0 votes

The jQuery inArray() method is used to find a specific value in the given array. If the value found, the method returns the index value, i.e., the position of the item. Otherwise, if the value is not present or not found, the inArray() method returns -1. This method does not affect the original array.

answered Jun 1, 2022 by Edureka
• 13,670 points

Related Questions In JQuery

0 votes
1 answer

jQuery.inArray(), how to use it right?

inArray returns the index of the element in ...READ MORE

answered Jun 21, 2022 in JQuery by rajatha
• 7,640 points
1,382 views
0 votes
1 answer

How to use $(document).ready equivalent without jQuery

Hello @kartik, Using DOMContentLoaded that is supported by over ...READ MORE

answered Sep 10, 2020 in JQuery by Niroj
• 82,880 points
1,206 views
0 votes
1 answer

How to make $.post() use contentType=application/json in jquery?

Hii, Try this: $.ajax({ url:url, type:"POST", ...READ MORE

answered Nov 24, 2020 in JQuery by Niroj
• 82,880 points
9,908 views
0 votes
1 answer

Is it possible to use jQuery .on and hover?

jQuery hover() Method The hover() method specifies two ...READ MORE

answered Jun 10, 2022 in JQuery by gaurav
• 23,260 points
577 views
0 votes
1 answer

Using setTimeout to delay timing of jQuery actions

The JavaScript setTimeout () function  is used ...READ MORE

answered May 30, 2022 in Java by gaurav
• 23,260 points
7,178 views
0 votes
1 answer

Best cross-browser method to capture CTRL+S with JQuery?

$(window).keypress(function(event) { if ...READ MORE

answered May 30, 2022 in JQuery by gaurav
• 23,260 points
1,216 views
0 votes
1 answer

Which keycode for escape key with jQuery

27 is the code for the escape key. ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,670 points
2,599 views
0 votes
1 answer

JQuery opposite of extend (reduce objects)

There is no such function, but you ...READ MORE

answered Jun 2, 2022 in JQuery by Edureka
• 13,670 points
370 views
0 votes
1 answer

How to change color of SVG image using CSS (jQuery SVG image replacement)?

Edit your SVG file, add fill="currentColor" to ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,670 points
21,485 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP