0

I need to check if a variable in if condition as follows.

if(table_block_id == ('customer_details' || 'billing_details' || 'shipping_details')){
    }

I know it's the wrong method . Is there any other way to check all values in a single line ?

5
  • 1
    Possible duplicate of A jQuery 'if' condition to check multiple values Commented Jan 22, 2018 at 12:24
  • it's not duplicate. I am having same variable and four values. Please read my question Commented Jan 22, 2018 at 12:25
  • It start with Possible Commented Jan 22, 2018 at 12:27
  • if(table_block_id == 'customer_details' || table_block_id == 'billing_details' || table_block_id == 'shipping_details') Commented Jan 22, 2018 at 12:28
  • you can also use array based to check the values. Commented Jan 22, 2018 at 12:30

3 Answers 3

2

The easiest way would be to use Array.prototype.includes as EKW suggested in his answer.

However, due to its poor support I would recommend using Array.prototype.indexOf instead:

if(["customer_details", "billing_details", "shipping_details"].indexOf(table_block_id) !== -1){
    // code
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, nice catch!
1

There are a few methods you could use. I quite like the following:

if(["customer_details", "billing_details", "shipping_details"].includes(table_block_id)){
    // code
}

1 Comment

Thanks . It's working. You are awesome than the rest of people mentioning duplicate for everything they see
1

You can put the options in array and use jQuery $.inArray() or javascript indexOf() to search array.

var a = 'customer_details';
    arr = ['customer_details', 'billing_details', 'shipping_details'];

    if($.inArray(a, arr) != -1) // With jQuery

        //code
    else
        //code

2 Comments

Ok ,@melvin. Try this solution. If its helpful mark it up.
Ok. Sure . I will

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.