Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

$.inArray()

In the chapter Form Validation and Manipulation > jQuery Utility Methods we used $.inArray(). I find this syntax $.inArray quite strange because it uses the selector $ without parentheses.

Also, and possibly related, in the jQuery documentation I noticed that some methods have jQuery in front of them (like jQuery.each) while others don't (like .add).

Whats up with that?

4 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

I wrote a post here on the Treehouse blog that discusses how the $ is an alias for jQuery – they are the same thing.

When you call a method on jQuery like jQuery.inArray() you're doing it on jQuery itself not jQuery selected elements. This is why they're called Utility Methods as they don't fit in the normal selector syntax. Their job is to simplify or smoothing out JavaScript implementations. It doesn't following the selector syntax as they are not performing an operation on the document's elements.

Thank you! That cleared things up quite a bit.

I hat jQuery!!!!! lol

here's my code.

var values = $("input.required").map(function(){
return $(this).val();})
return $.inArray("Andrew", values) != -1;

Does the $.inArray have to be inside the function? if so this code still doesn't work I also tried. return $.inArray(true, values) == "Andrew;

I don't know why Markdown isn't working

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

I added another line between the "here's my code line" from the code and that fixed the markdown.

The issue is is that you are returning on the last line. The return keyword cannot be used outside of a function call. Remove the return and you'll be good to go.