- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2022 08:00 PM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2022 08:05 PM
Hi
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2022 08:05 PM
Hi
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2022 10:32 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2022 02:47 AM
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 thestr
does not contain thesubstr
.
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
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2022 03:36 AM
let is an ECMAScript 6 feature and therefore doesn't work in server scripts!
Kind regards
Maik