Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use logical or operator with multiple values?

How to simplify the code when using multiple or operators.
I have a list of numbers from 0 to 6 separated by logical or.Is there any way to simplify it?

if (filteredMnth === 'mnth') {
         
  return (new Date(exp?.date).getMonth().toString() === "0" || "1" || "2" || "3" || "4" || "5" || "6"   )

}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A nice pattern for this, is to create an Array of valid or accepted values and use the Array.prototype.includes to check for one from the user input:

const validValues = [ 0, 1, 2 ];
const input = 2;

validValues.includes(input);
// => true

const input2 = 3;

validValues.includes(input2);
//=> false

As @Robby Cornelissen already mentioned in the comment, in your case it really does not make any sense, but I’m including this pattern here to answer a more generic version of your question.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading