0

In JavaScript what does the "> -1" mean when used in an if statement? for example:

if(window.location.href.indexOf("about") > -1) {
  //code
}
wallrouse
  • 116
  • 1
  • 13
  • 2
    The same as outside of an if statement. – Bergi Feb 21 '17 at 21:00
  • the important think is index of. it normally looking to more then localtion.href is not zero – Aniruddha Das Feb 21 '17 at 21:02
  • You couldn't Google this?https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf – Scott Marcus Feb 21 '17 at 21:08
  • It's a comparison. The result is `true` if the value on the left is larger than `-1`. `>` means "larger than". – Felix Kling Feb 21 '17 at 21:09
  • @ScottMarcus please try to be more constructive with your comments. I'm a newbie, that's why I'm asking questions. – wallrouse Feb 21 '17 at 21:25
  • I think my comment is quite constructive and is actually very appropriate for a newbie. You don't think that looking up the documentation for the method you have a question about makes sense? Does the information at that link, specifically the part that says: *" Returns -1 if the value is not found."* in the second sentence, not help you? – Scott Marcus Feb 21 '17 at 21:52
  • @ScottMarcus thank you for taking an interest in my question but if you read the question again, you will see it is not asking about a method. I feel you are confused by your interpretation of the question. Let me expand on the details of why I asked the question: I saw this code on another user's answer to a question I had asked, I left a comment asking the user the same question that was asked on this post, after not receiving an answer for 2 days I googleed my question, I did not find an answer, since I didn't find an answer on my own I reached out to the community. Thank you for the link! – wallrouse Feb 21 '17 at 22:04
  • There is no misinterpretation. The `-1` is used (regardless of an `if` statement) to test whether the `indexOf()` string *method* found a match or not. This question is directly related to the `indexOf()` method, hence my comment and my link. The article, if you read it, explains this quite clearly (and starts off with *"The indexOf() method"*). This is also why every other answer posted here is explaining the `indexOf()` method to you. – Scott Marcus Feb 21 '17 at 22:16
  • @ScottMarcus I totally agree that -1 is used to test whether the indexOf() string method found a match or not, but your answer was lacking a bit. If I'm being honest it is more of a question than an explanation and definitely lacked context. If I may make a helpful suggestion, next time add your comment as an answer to my question. That way you can be a little more explanatory with your thoughts. With that said I appreciate you taking the time to explain your thoughts! – wallrouse Feb 21 '17 at 22:27
  • I think you should spend more time trying to understand what Stack Overflow is for and how it works. When you say *"but your answer was lacking a bit"* it's because I didn't post an answer, I posted a comment. You already had 3 answers. I was simply adding the definitive resource for you to go investigate that would tell you everything you need to know to understand what's going on. Instead, you've been argumentative about my understanding of your question and my terminology, when all along it really has been you that just needs to listen to the correct information that we're giving you. – Scott Marcus Feb 21 '17 at 22:49
  • @ScottMarcus I think you misunderstood my tone as argumentative and it sounds as if you are getting lost in semantics. We are merely having a friendly conversation. Please take a deep breath Scott. In case you have not read all my replies, I have agreed with you on all your points and have also given you some helpful tips. I feel I have a good idea of what Stack Overflow is for and how it works though, but I appreciate you trying to help! SO is a community of programmers to learn and share (http://stackoverflow.com/company/about). I feel like we can both learn from each other if we are open – wallrouse Feb 21 '17 at 23:16
  • Well, when you say *" if you read the question again, you will see it is not asking about a method. I feel you are confused by your interpretation of the question"* you are not really agreeing with me and are being argumentative. Also, when you say *"next time add your comment as an answer to my question"* when my comment is not suitable as an answer and wasn't meant to be, you do telegraph that you actually do not understand how SO works. So, I would suggest you take a deep breath and read and try to understand what's being said before you respond. – Scott Marcus Feb 21 '17 at 23:35
  • @ScottMarcus I feel like there is still something you're missing here. Sorry if I was unclear before but I have read your answers thoroughly, which is why I felt the need to address some of your points. Also if you mean Argumentative as defined by google (https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=argumentative+definition) then yes I replied to you with a divergent view point, one different than yours. It's okay to have people not agree with you or offer feedback. We are here on SO to better ourselves and others. I hope you can agree with me on that – wallrouse Feb 21 '17 at 23:56

3 Answers3

2

This is just doing a detection of the string about in the URL. If about is found in the URL, the block will execute.

The convention of indexOf functions is to return -1 when the item isn't found.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

indexOf returns -1 if the pattern could not found. If it was found, it returns the index of the first occurence.

Christoph
  • 50,121
  • 21
  • 99
  • 128
bin
  • 21
  • 4
1

indexOf returns -1 if the search fails and the index of the occurrence in the string if found...

SPlatten
  • 5,334
  • 11
  • 57
  • 128