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.

Jun 21 in JQuery by gaurav
• 13,460 points
10 views

1 answer to this question.

0 votes

inArray returns the index of the element in the array, not a boolean indicating if the item exists in the array. If the element was not found, -1 will be returned.

So, to check if an item is in the array, use:

if(jQuery.inArray("test", myarray) !== -1)
answered Jun 21 by rajatha
• 4,940 points

Related Questions In JQuery

0 votes
1 answer

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

The jQuery inArray() method is used to find ...READ MORE

answered Jun 1 in JQuery by Edureka
• 13,640 points
10 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,720 points
263 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,720 points
2,667 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
• 13,460 points
7 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,500 points
294 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,720 points
8,335 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,720 points
15,186 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,720 points
1,186 views
0 votes
1 answer

How to use npm jquery module?

To use jquery in node, you need ...READ MORE

answered Jun 21 in JQuery by rajatha
• 4,940 points
14 views
0 votes
1 answer

How to open a Bootstrap 4 modal dialog using jQuery

Use the modal('show') Method You can simply use ...READ MORE

answered 2 days ago in JQuery by rajatha
• 4,940 points
18 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