Boo Who - is value a boolean primitive?

Boo Who - is value a boolean primitive?
0

Hi guys,

I’m working through the Basic Algorithm challenges in JavaScript, and have come across what looks like a relatively simple one: check if a value is a primitive boolean.

I know to check the value with typeof, but I was checking if the value was equal to the keyword boolean, and I can see the solution says I need to check if it is equal to the string boolean.

I can’t figure out why? Why am I checking if it is equal to a string??

I’m sure it’s a simple answer but I am a novice so please go easy on me!

Thanks!

The typeof operator returns a string indicating the type of the unevaluated operand.

For example:

  • typeof 1 === 'number'
  • typeof undefined === 'undefined'
  • typeof true === 'boolean'
  • typeof null === 'object' (yep, really… due an old implementation detail that can’t be changed for fear of breaking the web)
1 Like

Thanks Lionel :slight_smile:

1 Like

I personally use:

Object.prototype.toString.call(YOUR_VALUE);

Object.prototype.toString.call(null)
"[object Null]"
Object.prototype.toString.call(3)
"[object Number]"
Object.prototype.toString.call('Test')
"[object String]"
Object.prototype.toString.call(undefined)
"[object Undefined]"
Object.prototype.toString.call(true)
"[object Boolean]"