What does (xxxx > -1) mean in a script?

User657785
Tera Contributor

Hi There,

What does the "-1" mean in JavaScript? I have tried google but not getting much information back on it.

Here is a code snippet (example)

if(modDesc.indexOf("/knowledgebase.do?uri=kb_view.do?sys_kb_id") > -1){
		modDesc = modDesc.split('/knowledgebase.do?uri=kb_view.do?sys_kb_id');
		modDesc = modDesc.join('?id=kb_article&sys_id');
		gs.print(gr[name] + ' - knowledgebase.do sys_kb_id links updated');
		updated ++;
	}

 

Thank you in advance for your help.

Ty

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Hi @Tyrone 

the JavaScript function indexOf() returns the position of a given String within another String. If nothing could be found, "-1" is returned. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf for more information.

Regarding your example it means, that only in case "/knowledgebase.do?uri=kb_view.do?sys_kb_id" is contained within the variable modDesc the code within the if condition is executed.

Kind regards
Maik

View solution in original post

5 REPLIES 5

Maik Skoddow
Tera Patron
Tera Patron

Hi @Tyrone 

the JavaScript function indexOf() returns the position of a given String within another String. If nothing could be found, "-1" is returned. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf for more information.

Regarding your example it means, that only in case "/knowledgebase.do?uri=kb_view.do?sys_kb_id" is contained within the variable modDesc the code within the if condition is executed.

Kind regards
Maik

Saurav11
Kilo Patron
Kilo Patron

Hello,

What it means is the value in variable modDesc does it contain the string mentioned in the bracket. If it contains it will return a value greater than 1 so it will go inside the if look.

If it does not contain then the string that means the value returned is -1 which is not greater and the condition is not satisfied so it will not enter the loop.

Please mark answer correct/helpful based on Impact.

shloke04
Kilo Patron

Hi,

The String.prototype.indexOf() returns the index of the first occurrence of substring (substr) in a string (str😞

let index = str.indexOf(substr, [, fromIndex]);
It returns -1 if the str does not contain the substr.

The fromIndex is an optional parameter that specifies the index at which the search starts. It defaults to zero (0), meaning that if you omit the fromIndex, the search will start from the beginning of the string.

The indexOf() always perform a case-sensitive search.

To find the index of the last occurrence of a substring in a string, you use the lastIndexOf() method.

 

Refer to below link to understand with example of how to use indexOf method.

https://www.javascripttutorial.net/javascript-string-indexof/

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

let is an ECMAScript 6 feature and therefore doesn't work in server scripts!

Kind regards
Maik