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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Lee Howard](https://uploads.teamtreehouse.com/production/profile-photos/6081792/micro_2014-06-14_19.09.18.jpg)
Lee Howard
6,621 PointsWhats the purpose for the ( search ) > -1?
I'm just really confused as to why he uses it. Dave mentions that if the value is not in the array then the interpreter returns the value -1 and if the index0f is greater than -1 then the value is in the array..
Why are we even checking the value? I don't see any number being printed to the page. I'm just really confused as to why it's even in there!!
4 Answers
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsYou didn't provide a link to the video, so I can't quote directly from it, but clearly the purpose of the test is to determine if the item that was searched for was actually found in the array. It might not be important to the program to print anything at this point, but the code in the block following this test is quite likely dependent on having found the term in the array.
To facilitate the most complete and accurate answers, always provide a link to the course page.
![Jennifer Nordell](https://uploads.teamtreehouse.com/production/profile-photos/5432702/micro_jen-square.jpg)
Jennifer Nordell
Treehouse TeacherHi there! I wish I knew specifically to which video you were referring but let's try my own made-up example. What if I had a list of names of friends that I invited to a party. Let's say it's a pretty big and expensive party. Anyone not on that list is not allowed admittance. I've hired someone to watch the entry point to the party to make sure no one who isn't allowed comes in.
If I had an array with the lists of names of people that were allowed in and did a search against that array, if it returned a -1 I would know that they are not on the list and not allowed in. If the search shows any other number, they are allowed in.
Hope this helps!
![Lee Howard](https://uploads.teamtreehouse.com/production/profile-photos/6081792/micro_2014-06-14_19.09.18.jpg)
Lee Howard
6,621 PointsSorry guys! I thought because I asked a question while watching the video it would of included it in the message!
https://teamtreehouse.com/library/useful-array-methods - here's the video I'm talking about. I hope that enables you to help me now :) Thank you!
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThat helps! I added a comment to complete my original answer.
![Lee Howard](https://uploads.teamtreehouse.com/production/profile-photos/6081792/micro_2014-06-14_19.09.18.jpg)
Lee Howard
6,621 PointsThanks Steven! I actually understand what you mentioned already. It's the " > -1 " that is confusing me. :/ :????
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThe expression "> -1
" creates a test that asks the question: "Is it greater than negative 1?".
If the indexOf function finds the searched item, it will return the index number of the array element where the item was found. And array indices start at 0 and go up. But if the function does not find the item, it returns -1 instead.
So by testing if the returned value is greater than -1, the test result will be true if the item was found, and false if the item was not.
Steven Parker
230,688 PointsSteven Parker
230,688 PointsSo, to understand the purpose for the test we need to look at a bit more of the code from the video:
As I said, the test determines if the item is found in the array. From the additional code you can see that when it is found, the program will print "Yes, we have name_of_item in the store.".
When it is not found, the "else" clause will be executed instead and print "name_of_item is not in stock.".