Solution for the freeCodeCamp lab “Build a Boolean Check Function.”
Write a function booWho that:
- Receives one argument.
- Returns
trueif the argument is a boolean primitive (trueorfalse). - Returns
falsefor any other value (numbers, strings, arrays, objects,NaN, etc.).
See booWho.js.
function booWho(bool) {
return typeof bool === "boolean";
}How it works: typeof returns the string "boolean" only for the two
boolean primitives, true and false. For every other type, typeof
returns something else ("number", "string", "object", etc.), so the
function returns false.
| Input | Output |
|---|---|
booWho(true) |
true |
booWho(false) |
true |
booWho([1, 2, 3]) |
false |
booWho([].slice) |
false |
booWho({ "a": 1 }) |
false |
booWho(1) |
false |
booWho(NaN) |
false |
booWho("a") |
false |
booWho("true") |
false |
booWho("false") |
false |