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

There are two approaches:

Method 1 (!==not equal)    //recommend 

var str_1 ="test 1";

var str_2 = "test 2";

 

if (str_1 != str_2 ){  //true

    alert("str_1 != str_2");

}else{

  alert("str_1 == str_2");

}

 

Method 2 (!== not equal value or not equal type)

if (str_1 !==str_2 ){  //true

//do something

}else{

//do something

}

 

Geoff_T
Mega Sage

A nice alternative using ternary operator:

var isNotEqual = "string1" !== "string2" ? "true" : "false";
gs.log(isNotEqual);

 

Geoff

Please let me know if this resolved your query.

Geoff