Is there a shorter, cleaner way to match explicit true or false values only in JS? No match for undefined or anything else.
if (var1 == true|| var1==false){}
Is there a shorter, cleaner way to match explicit true or false values only in JS? No match for undefined or anything else.
if (var1 == true|| var1==false){}
Use typeof
instead:
if (typeof var1 === 'boolean') {
}
Note that you should not use sloppy comparison with ==
- always use ===
(and cast types explicitly if needed) if you want to avoid weird auto-coercion bugs.