myHotTake

Why Are Strict Null Checks Essential in JavaScript?

If you find this story engaging and helpful, feel free to like or share it with others who might appreciate it!


I have a backpack that can hold anything I need throughout the day. This backpack is amazing, but there’s a catch: it can only carry items that I have explicitly told it about. If I forget to mention an item or assume it’s in there without checking, I could reach inside and find nothing but empty space when I need it most.

In the world of JavaScript, strict null checks are like that backpack. They ensure that I’m always aware of what I’ve packed and what I haven’t. By enabling strict null checks, I’m making a pact with my backpack to always be clear about what should be inside. If I try to pull out my water bottle, but I forgot to pack it, the backpack will immediately let me know with a gentle reminder rather than leaving me thirsty later on.

This becomes crucial when I’m depending on various items throughout my day—like my phone, keys, or wallet. If I’m not diligent, I might assume I have everything, only to find myself locked out of my house or unable to call a friend. Similarly, in JavaScript, if I assume a variable has a value when it doesn’t, I could run into errors that stop my program in its tracks.

By using strict null checks, I’m encouraged to double-check my backpack’s contents at every step. It makes me more mindful about what I pack and ensures I’m never caught off guard. This way, I can move through my day with confidence, knowing that everything I need is actually there.

If you enjoyed this analogy, liking or sharing it would be awesome! It might just help someone else understand this concept too.


I have a function that fetches my favorite book from the backpack:

function getFavoriteBook(backpack: { favoriteBook?: string }): string {
  return backpack.favoriteBook;
}

Without strict null checks, I might assume the book is always packed. But if I reach for it and it’s not there, my program will throw an error, much like reaching into an empty backpack.

By enabling strict null checks in TypeScript, like so:

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

I am prompted to handle the possibility that my favoriteBook might be undefined. This means I need to check if the book is there before trying to read it:

function getFavoriteBook(backpack: { favoriteBook?: string }): string {
  if (backpack.favoriteBook) {
    return backpack.favoriteBook;
  } else {
    throw new Error("Favorite book is not in the backpack!");
  }
}

With this setup, I’m always reminded to verify the presence of my favorite book before trying to read it, preventing unexpected errors.

Here’s another example with nullish coalescing:

function getFavoriteBookTitle(backpack: { favoriteBook?: string }): string {
  return backpack.favoriteBook ?? "No book found";
}

In this case, if the favoriteBook isn’t there, I’m prepared with a default message, just like having a backup plan if I forget to pack my book.

Key Takeaways:

  • Awareness: Enabling strict null checks in TypeScript forces us to be explicit about our assumptions, preventing unexpected runtime errors.
  • Safety: Just like checking my backpack before leaving the house, strict null checks ensure that I handle potential null or undefined values gracefully.
  • Confidence: With strict null checks, I can write more reliable code, knowing that I’ve accounted for all scenarios, much like having confidence that my backpack contains everything I need.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *