Skip to content Skip to sidebar Skip to footer

Is There Any Clean Way To Code A Group Of || Testing To See If The Value Of A Variable Is In One Of Them?

I have code like this: if (view == 'delete' || view =='edit' || view == 'new') {} Is there a more clean way that I can code this without using an external library. It just does n

Solution 1:

Use indexOf (won't work on ancient IE versions):

if (["delete", "edit", "new"].indexOf( view ) >= 0) { alert("ME");}

Solution 2:

You can also use regular expression:

if (/^(delete|edit|new)$/.test(view)) {
    // true
}

Post a Comment for "Is There Any Clean Way To Code A Group Of || Testing To See If The Value Of A Variable Is In One Of Them?"