Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if.. comparison.. What is -1 1

Status
Not open for further replies.

sdi

Technical User
Jun 25, 2003
65
US
I have read and read and just am not getting it. What exactly is -1 telling me?

Example 1:

if(navInfo.indexOf("Win98" != -1))
{
//do something
}

Example 2:

var myString = "something old, something new, you know the rest"
var foundAtPosition = 0;
var myCount = 0;
while(foundAtPosition != -1)
{
foundAtPosition = myString.indexOf("something", foundAtPostion);
if(foundAtPosition != -1)
{
myCount++;
foundAtPosition++;
}
}

document.write("There are " + myCount + " occurances of something");
}


Thank you in advance,

Brian
 
The JavaScript function indexOf searches a string for a specified substring, and returns the character position where the substring is first found. If the substring is not found, it returns a -1. The comparisons in your script block examples are testing to see if the substring is found by checking that the return from various indexOf calls are not -1.

Found this information at
HTH,
jp
 
Thank you for the reply. I think I am beginning to grasp it.

**If the substring is not found, it returns a -1**

That put it all in prospective for me and the link will be quite handy in the future.

Again, Thank you!

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top