JavaScript - Check if a value is classified as a boolean primitive


Check if a value is classified as a boolean primitive. Return true or false.


Solution 1 Basic approach


console = { 
    log: print,
    warn: print,
    error: print
};
function booWho(bool) {
    if (bool === true || bool === false) {
        return true;
    }
    return false;
}
console.log(booWho());


Output:


false


Solution 2 one-liner approach


console = { 
    log: print,
    warn: print,
    error: print
};
function booWho(bool) {
    return (bool === true || bool === false)
}
console.log(booWho(false));



Output:


true


Solution 3 Using typeof


console = { 
    log: print,
    warn: print,
    error: print
};
function booWho(bool) {
    return typeof bool === "boolean";
}
console.log(booWho(null));



Output:


false