---
title: "Why null checks are bad"
url: https://daily.dev/posts/why-null-checks-are-bad-mcmdvw2r0
source_url: https://swizec.com/blog/why-null-checks-are-bad
type: article
source: "JSK"
published: 2021-12-13T15:35:54.067Z
updated: 2021-12-13T15:35:54.068Z
tags: ["testing"]
reading_time: 5
upvotes: 55
comments: 6
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Why null checks are bad

**[JSK](https://daily.dev/sources/jsk)** · 5 min read · 55 upvotes · 6 comments

## Summary

css-s4hmgy.css-15e6h9m {font-family:monospace;font-size:14px;overflow-x:auto;background-color:var(--theme-ui-colors-muted);padding:16px;border-radius:4px;}.css-863l4v .comment,.css-6.5.3.1:hover, .css-7.2.3:hover,.css.8.3-3.4:hover;.css.9.4.1.0:hover.css:8.5%.css:9.5%;.css

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://swizec.com/blog/why-null-checks-are-bad>

## Community discussion

Top comments from developers on daily.dev.

**@andrei0x309** · 4 upvotes

> Null checks are totally fine when you are 100% sure that what you check it returns null, many programing languages have core functions that return null when something fails, or if something is not found, is an old pattern.
>
>
> So in the case of JS and other dynamic type languages is only a problem when you get some other falsy value you don't expect, and if you have a large code base you could use typescript which will catch that, but generally in JS you do a check for all falsy values communally is !!value.

**@matteosacchetto** · 2 upvotes

> Hi everyone, I just wanted to say that instead of using `||` to provide a default if a value is null, it is better to use th e `??` operator. Simple case which lead to a different behavior that expected are with numbers (`0 || 3` returns `3`, while `0 ?? 3` returns `0`), strings (`"" || "Unknown"` returns `"Unknown"`, while `"" ?? "Unknown"` returns `""`), and so on..
> So remeber always to use the right operator based on the situation 😉

**@jonrose** · 1 upvotes

> I'm going to disagree here with this advice. It's too simplistic to be useful. Sure in these contrived examples, not allowing for null is helpful. But in larger projects, null is a reality. There will be optional properties within objects, and your code will need to be able to handle them. Trying to insert some default value for each optional field will result in far more complex code than simply being able to handle nulls.
>
>
> Imagine you have a user object with an optional birthday field. In the user profile, you may want to show "Unknown" for the birthday, but now that means your birthday...

**@atur** · 1 upvotes

> Awesome explanations and examples! <3

**@alvbarros** · 0 upvotes

> I'm not really familiar with large scale React projects, but are you really supposed to test the whole component?
> Maybe break a test in a bunch of tests for every variable.
>
>
> For example, have generic methods for testing that would be called, like if an object has a null property, check if it contains the specific Component for when it is null.
>
>
> I don't know. I don't think this is a problem with null checks, more so a problem with how you are developing your tests.

## Similar posts on daily.dev

- [Don’t just attend KubeCon \+ CloudNativeCon, Merge Forward your experience\!](https://daily.dev/posts/don-t-just-attend-kubecon-cloudnativecon-merge-forward-your-experience--l0rpp73x8) · CNCF · 0 upvotes · 0 comments
- [Announcing H2 2026 KCDs](https://daily.dev/posts/announcing-h2-2026-kcds-m96goajm1) · CNCF · 1 upvotes · 0 comments
- [Two months of Open Community Groups](https://daily.dev/posts/two-months-of-open-community-groups-asf52zhbs) · CNCF · 0 upvotes · 0 comments

---

Tags: [#testing](https://daily.dev/tags/testing)

[View this post on daily.dev](https://daily.dev/posts/why-null-checks-are-bad-mcmdvw2r0)
