FCC Bonfire Series 126: Boo who

We are going to get it done FAST today. This next bonfire, Boo Who will have us write a function that given a value, return true if that value is a boolean primitive (true or false) and false otherwise.

Remember that we are checking for primitive boolean values, not truthy or falsy ones.

Let’s get straight to the action by using the typeof operator. Remember that typeof operates over a value and returns it’s type:

typeof 'hello world';  //-> "string"
typeof 4;  //-> "number"
typeof true;  //-> "boolean"

Let’s start by using a conditional if statement:

function boo(value) {
  if (typeof value === 'boolean') {
    return true;
  } else {
    return false;

Pretty simple isn’t it! Let’s make it a little cleverer (I just found out that cleverer is an actual word).

function boo(value) {
  return typeof value === 'boolean' ? true : false;

And that’s it! You may wanna come up with your own version for this function, but this time, there isn’t as much room for variation. Stay cafeinated and sharp, and may the force be with you.