If you just want to check for a primitive value
typeof variable === 'boolean'
If for some strange reason you have booleans created with the constructor, those aren't really booleans but objects containing a primitive boolean value, and one way to check for both primitive booleans and objects created with new Boolean
is to do :
function checkBool(bool) {
return typeof bool === 'boolean' ||
(typeof bool === 'object' &&
bool !== null &&
typeof bool.valueOf() === 'boolean');
}
function checkBool(bool) {_x000D_
return typeof bool === 'boolean' || _x000D_
(typeof bool === 'object' && _x000D_
bool !== null &&_x000D_
typeof bool.valueOf() === 'boolean');_x000D_
}_x000D_
_x000D_
console.log( checkBool( 'string' )); // false, string_x000D_
console.log( checkBool( {test: 'this'} )); // false, object_x000D_
console.log( checkBool( null )); // false, null_x000D_
console.log( checkBool( undefined )); // false, undefined_x000D_
console.log( checkBool( new Boolean(true) )); // true_x000D_
console.log( checkBool( new Boolean() )); // true_x000D_
console.log( checkBool( true )); // true_x000D_
console.log( checkBool( false )); // true
_x000D_