Hi All,
I want to use IN Operator in Javascript
e.g : if (cType IN ('SK','SK 2'...)
Regards
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sandeep Singh ShekhawatRank #147 · 15.6K reputationPosted Mar 26, 2013, 10:45 PM EDT
function getValue()
{
var typesArray = new Array("SK", "SK 2", "SK 4","XYZ TYPE");
var cType = 'XYZ TYPE';
for (var j = 0; j < typesArray.length; j++)
{
if (typesArray[j].match(cType))
{
alert("It exists in array");
}
}
}
Sandeep Singh ShekhawatRank #147 · 15.6K reputationPosted Mar 26, 2013, 8:27 PM EDT
for example:
<script type="text/javascript">
function InOperator()
{
var typesArray = new Array("SK", "SK 2", "SK 4");
var cType=1;
if (cType in typesArray)
{
alert("It exists in array");
}
}
script>
Thanks
Sandeep Shekhawat
ShehzadRank #2,660 · 522 reputationPosted Mar 26, 2013, 9:07 PM EDT
Index will not work for cType NOT IN
Regards
Sandeep Singh ShekhawatRank #147 · 15.6K reputationPosted Mar 26, 2013, 9:21 PM EDT
Hi Shehzad,
You can't use NOT IN operator in if condition because javascript doesn't support it. If you want use NOT IN then you can use like following code:
function NotInOperator()
{
var typesArray = new Array("SK", "SK 2", "SK 4");
var cType = 4;
if (!(cType in typesArray))
{
alert("It does not exist in array");
}
}
Thanks
Sandeep
ShehzadRank #2,660 · 522 reputationPosted Mar 26, 2013, 10:23 PM EDT
for e.g. : cType = "XYZ TYPE" // cType will will be assigned as per user input from InterFace.
How will i check whether "XYZ TYPE" is exists or not in typesArray?
Regards
ShehzadRank #2,660 · 522 reputationPosted Mar 26, 2013, 10:48 PM EDT
Regards
ShehzadRank #2,660 · 522 reputationPosted Mar 26, 2013, 11:34 PM EDT
I have done some modification as per my requirement
var typesArray = new Array("SK", "SK 2", "SK 4","XYZ TYPE");
var cType = 'SK 2';
for (var j = 0; j < typesArray.length; j++)
{
if (typesArray[j] == cType)
{
alert("It exists in array");
break;
}
}