query multiple values in if condition

Viewed 70

I have a dropdown with 2 values. Option 1, Option 2 I want to include both these values in the jquery function. If condition works fine with the single value. but how to write multiple values in the same if condition.

My code is

jQuery(function($){
if(selectedval == "Option 1")

Note: selectedval is a the name of the variable.

1 Answers

You can simply add a logical or to your condition:

selectedval == "Option 1" || selectedval == "Option 2"

It also works for more than two operands, you can add as many as you want. Have a look at the details here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR

The upper one is true if at least one of the operands is true. When you need all of them to be true, you can use the logical and operator &&:

a == 1 && b == 1 && c == 5
Related