Basic useage:
The function is used to return index where the specific string first appeared.If there is no such string,return -1.
<script>
var str="hello world";
console.log(str.indexOf("lo")); //3
console.log(str.indexOf("ol")); //-1
</script>
Abstract leakage:
But if we want to get whether there has such string in the whole string,we can not write like this:
if(str.indexOf("lo")>=0)
{
console.log("match");
}
if(str.indexOf("ol")==-1)
{
console.log("no match");
}
For we expose the underlying implementation details ,if so.
We need to write like this:
if(~str.indexOf("lo"))
{
console.log("match");
}
if(!~str.indexOf("ol"))
{
console.log("no match");
}