Can someone please explain to me what does the -1 mean/represent in this if statement.
if(window.location.href.indexOf("pathname") != -1) {
//do something
};
The way I think would be to do something like (if true then do something)
if(window.location.href.indexOf("pathname") == 0) {
//do something
};
0 being 1? That means it's always false unless 'pathname' exists which bees true?
This confuses me all the time. Should I use != -1, == 0 or >=0.
Can someone please explain to me what does the -1 mean/represent in this if statement.
if(window.location.href.indexOf("pathname") != -1) {
//do something
};
The way I think would be to do something like (if true then do something)
if(window.location.href.indexOf("pathname") == 0) {
//do something
};
0 being 1? That means it's always false unless 'pathname' exists which bees true?
This confuses me all the time. Should I use != -1, == 0 or >=0.
Share Improve this question edited Jul 15, 2013 at 2:27 Lee Taylor 7,98416 gold badges37 silver badges53 bronze badges asked Jul 15, 2013 at 2:18 NickPNickP 3571 gold badge8 silver badges18 bronze badges 1- 1 First of all, have you find out how the indexOf() method works? Please go through some tutorial on it before asking question. – gjman2 Commented Jul 15, 2013 at 2:24
9 Answers
Reset to default 1indexOf is a function that locates the index (position) of an object in any other given object whether it be a char in a string, or a byte in a buffer... etc.
If it is not found, just for safety purposes it returns -1. This is because 0 is an index in the object.
For example:
"hello world"
h = 0
e = 1
l = 2
...
so if it is not found it will be -1, and if it is found it will be >= (greater than or equal to) 0. so essentially != -1 and >= 0 will return the same output.
Based on the docs, indexOf
returns -1 if it doesn't find a match of the string in your argument. Otherwise, it returns the index of your string.
Quote from docs:
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
-1 is used to indicate that there wasn't a match when searching for the substring you provided. All output > -1 indicates the start position of where the substring was found in the larger string
Read more here: http://www.w3schools./jsref/jsref_indexof.asp
indexOf
returns the index at which the substring begins in your string, not a true
/false
depending on the presence of the substring in your string.
Strings and arrays are zero-indexed in JavaScript, so a return value of 0
means that the substring begins at the very beginning of your string. -1
isn't a valid index, which is why indexOf
returns it when the string wasn't found.
indexOf returns -1 when the parameter string is not found. It also returns >=0 if it's found. So if you are checking whether the string occurs or not, !=-1 and >=0 is same thing. It returns 0 if match starts from the first index.
.indexOf()
is meant to look for a match - in your case of substring. If the function finds a match it returns the index in which it starts. If no match was found it returns -1. You can read the docs here.
As per your question:
if(window.location.href.indexOf("pathname") == 0) {
// code here will be executed only if your current URL
// starts with 'pathname' (0 is the first index in a String)
};
if(window.location.href.indexOf("pathname") != -1) {
// code here will be executed only if your current URL
// contains the string 'pathname' (-1 means no match was found)
};
-1 -- no such a string inside
0 -- the searchable string is in the very beginning (its first symbol is the 0's symbol in the subject string)
http://www.w3schools./jsref/jsref_indexof.asp
The indexOf() method returns the position of the first occurrence of a specified value in a string.
So use >=0 to check if substring is in the string and ==-1 to check that substring is absence
Check this link http://www.w3schools./jsref/jsref_indexof.asp
Please go through some tutorial and find out about the indexOf(). Btw, indexOf() is javascript not jQuery :)