Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to compare two string with not equal to ?

kajol1
Kilo Explorer

how to compare two string with not equal to ?

7 REPLIES 7

joshvanharn
Kilo Guru

<x> != <y>

Mahesh Kumar3
Giga Guru

Hi kajol,

Where are you stuck? What is your use case?

Generally we use the following script to compare two scripts:

 

var str1 = 'FirstString';
var str2 = 'SecondString';

if(str1==str2){
   // If equal then this block will execute
}else{
   // If unequal then this block will execute
}

Hi Mahesh,

Can I compare using a variable?

Example:

var compare = ">";

if(10 compare 20) {

   gs.print("10 > 20");

}

else {

   gs.print("10 < 20");

}

Isaac Vicentini
Mega Sage
Mega Sage

You can use the js indexOf() function:

 

var string = "Hello world, welcome to the universe.";
var containsString = string.indexOf("welcome");

if(containsString == -1){
console.log('Does not contain');
} else {
console.log('Contains');
}

 

The containsString variable will return -1 if it does not contain.

 

https://www.w3schools.com/jsref/jsref_indexof.asp


Best regards,

Isaac Vicentini
MVP 2025 ✨


If my answer was helpful, mark it as Helpful or Accept as Solution.