what does mean indexOf("searchString") == -1 this only for string or by alphabet also

Rajababu
Giga Guru

Hi ,

Can any one explain above term .

if indexOf("Gaurav") == -1 to indexOf("Gaurav") >-1 what it says

Line Gaurav is working in Servicenow or Servicenow working for gaurav or Every one knows gaurav working for Servicenow.

Can any one explain .nayan_awadhiya pradeepksharma

1 ACCEPTED SOLUTION

Gowrisankar Sat
Tera Guru

indexOf() refers to the Index where that particular string is found.



var string = testGaurav


Here: indexOf("Gaurav") returns 4.



var string = Gaurav


Here: indexOf("Gaurav") returns 0.



var string = testtest


Here: indexOf("Gaurav") returns -1, since the string is not found.



Which means, if string is found anywhere, it returns value starting from 0.


View solution in original post

11 REPLIES 11

Can we use any method alternative to indexOf() it's not working in my script im using this to fetch records in description field in assignment rule

A code sample would be the best way to get some help on your issue.
What is the type of the item you are calling indexOf() on? What is the type of the item you are using as the argument for indexOf()? Are they JavaScript or Java? The best way is to ensure that you convert all items to JavaScript so you are consistent. You can also use the string function of search() which allows you to use a RegEx. Both return integers so if the string you are using as the argument is not found it will return -1 in both cases. Don't use any comparison operators on that result since there are 4 options you can use ( x >= 0, x > -1, x == -1, x < 0) that can be confusing. I suggest you use the ~ operator which will do a 2's compliment on the result so that a not found result will be 0 instead of -1 since ~-1 is 0. Go ahead and press F12 and try it in the console. Now you can put the ~indexOf() or ~search() in an if statement and utilize the toBoolean result.