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 in JQuery by Edureka
• 13,640 points
29 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 by Edureka
• 13,640 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 in JQuery by rajatha
• 7,040 points
29 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,740 points
271 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,740 points
2,947 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 in JQuery by gaurav
• 17,000 points
24 views
0 votes
1 answer

Using setTimeout to delay timing of jQuery actions

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

answered May 30 in Java by gaurav
• 17,000 points
64 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 in JQuery by gaurav
• 17,000 points
83 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 in JQuery by Edureka
• 13,640 points
64 views
0 votes
1 answer

JQuery opposite of extend (reduce objects)

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

answered Jun 2 in JQuery by Edureka
• 13,640 points
14 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 in JQuery by Edureka
• 13,640 points
244 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP