True & False Are NOT Boolean!?

@CODE-NINJA’s advice is correct but I wanted to elaborate on this a bit more with a couple points as to why this is generally the correct way to do this, at least in JavaScript.

JS itself lacks strongly typed declarations and has no native notion of a ternary (or nullable) object. Instead every object can have either a value, null, or be undefined. If JS had type declarations we might be able to differentiate better using a nullable type like bool? which would explicitly declare that while a variable may have a boolean value it can also be null. But we don’t have this available to us.

Instead we leverage undefined, null, or both to represent certain edge cases. In this case entries in your database have 3 potential values for a boolean field:

  • true
  • false
  • Empty (aka undefined)

Empty is not false which is why we don’t want to assume and tell the frontend application that it’s false when it isn’t. Rather we tell frontend that it’s undefined which is an important piece of information. It lets us know that this particular field doesn’t have a value.

More practically then these values mean:

  • true = User checked the box
  • false = User unchecked the box
  • Empty (aka undefined) = User hasn’t engaged with the box

However in addition to doing the defensive programming that @CODE-NINJA mentioned above you can also do one of the following:

In the second option you’ll then get your queries returned so that the field is reported as the default value whenever it is empty. For example this database looks like:

But when we query it our items array says:

[
  {
    "boolean": false,
    "title": "test2"
  },
  {
    "boolean": false,
    "title": "TEST"
  }
]
1 Like