1

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

Boolean primitives are true and false.

function booWho(bool) {
     return bool;
    }
    booWho(null);

This are the tests:

booWho(true) should return true.
Waiting:booWho(false) should return true.
Waiting:booWho([1, 2, 3]) should return false.
Waiting:booWho([].slice) should return false.
Waiting:booWho({ "a": 1 }) should return false.
Waiting:booWho(1) should return false.
Waiting:booWho(NaN) should return false.
Waiting:booWho("a") should return false.
Waiting:booWho("true") should return false.
Waiting:booWho("false") should return false.

My tries:

function booWho(bool) {
      return (bool == true||false)
    }
    booWho(null);

  
    function booWho(bool) {
      return (bool == true|| bool == false)
    }
    booWho(null);

this two gives me a problem with this test: booWho(1) should return false

function booWho(bool) {
      return (bool === true||false)
    }
    booWho(null);


this one gives me a problem with this test: booWho(false) should return true.

I ended up passing with typeof, but i have no idea why the others are giving errors, thanks in advanced

Pointy
  • 405,095
  • 59
  • 585
  • 614
Meigu
  • 11
  • 2
  • I mean, `typeof bool === 'boolean'` will do the basic trick... – somethinghere Sep 01 '23 at 13:41
  • 2
    The `||` operator does not work the way some of your tests expect it to. If you don't want `typeof`, then comparing `bool` to `true` and `false` with `===` will work. – Pointy Sep 01 '23 at 13:41
  • But i don't understand why the `| |` doesn't work. When i was learning about it I was told it was a simpler/shorter way, but in some cases it works, but not in others, do you have a article or a recommendation for me to learn about why is that? Thanks! – Meigu Sep 01 '23 at 19:32
  • @Meigu I hope you understood the `strict equality` and what `falsy` and `truthy` values are. For the question why `bool === true || false` is working, actually is not working, it just work in the case where `bool` is `true` but if it's `false` or any other value it won't work, and that's because the expression is just `a || b` where `a` is `bool === true` and `b`=`false`, so when one of `a` or `b` is `true` it will be `true`, in your case when `bool === true` is `true`, so if you test with `bool=false`, this won't work.I hope you got it. – cнŝdk Sep 01 '23 at 21:24

2 Answers2

0

Use strict equality to prevent type coercion and separate the checks to true and false:

function booWho(bool) {
  return bool === true || bool === false;
}
starikcetin
  • 1,391
  • 1
  • 16
  • 24
0

Your last try is close but you need to check for strict equality using a triple equal (===) because non-boolean values can be truthy or falsy as well. === prevents implicit conversion.

function booWho(bool) {
  return bool === true || bool === false;
}

console.log(booWho(true));
console.log(booWho(false));
console.log(booWho(null));
console.log(booWho([]));
console.log(booWho({}));
console.log(booWho(() => void 0));
Behemoth
  • 5,389
  • 4
  • 16
  • 40
  • Thanks a lot! I didn't know about truthy or falsy values, thanks a lot for the info. I was breaking my head thinking why it randomly decided not to work in some cases. Do you know why `bool === true || false ` work? i thought it was the same but shorter that `bool === true || bool === false` Thanks again! – Meigu Sep 01 '23 at 19:35
  • No it's not just shorter syntax but has a different meaning. I suggest doing some research on how the [*logical or operator*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR) ( `||` ) works. – Behemoth Sep 02 '23 at 10:49