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

Solution not working or have any suggestions? Please send an email to [email protected]



Join Our Facebook Group


Share this example


Download Android App

download android app

Popular javascript Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube