How to use OR condition in a JavaScript IF statement?

Use the logical “OR” operator, that is ||.

if (A || B)

Note that if you use string comparisons in the conditions, you need to perform a comparison for each condition:

if ( var1 == "A" || var1 == "B" )

If you only do it in the first one, then it will always return true:

if ( var1 == "A" || "B" ) //don't do this; it is equivalent to if ("B"), which is always true

The official ECMAScript documentation can be found here

Leave a Comment