What is the proper way to check for null values?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
There is no single universal null check that works the same way in every language or every data system. The proper way depends on both the platform and your intent: do you want to detect an absent reference, distinguish null from other empty values, or write a query that handles missing data correctly.
Start With the Meaning of "Null"
Programmers often mix several ideas together:
- a missing reference such as
nullorNone - an empty value such as
"",[], or0 - an undefined variable or missing property
- a database
NULL
Those are not interchangeable. The first step in "proper" null checking is deciding which one you actually mean.
Null Checks in Common Languages
In Java, checking whether a reference is missing is direct:
For ordinary reference checks, == null is the normal and readable choice. Avoid calling methods before that check.
In Python, the idiomatic test is is None, not == None:
is None tests identity, which is the intent for Python's singleton None.
In JavaScript, the right check depends on whether you want to treat null and undefined the same way:
The loose equality form == null is one of the rare cases where non-strict comparison is intentionally useful, because it matches both null and undefined without also matching empty strings or zero.
Database Null Checks Are Different
SQL NULL does not behave like ordinary values. Comparing with = does not work. You must use IS NULL or IS NOT NULL.
This is a common source of bugs for developers who move between application code and SQL. email = NULL is not true; it evaluates to unknown in SQL's three-valued logic.
Prefer Early Checks and Clear Contracts
Null handling is easier when you decide at API boundaries what is allowed. For example, instead of checking for null everywhere in the middle of the code, validate inputs early:
This makes failures immediate and easier to debug.
In Python, the same idea can be expressed with guard clauses:
The best null check is often the one that narrows the scope of nullable values as early as possible.
Null-Safe Access Patterns
Sometimes you do not want a hard failure; you want safe access with a fallback.
In JavaScript:
This avoids throwing when an intermediate property is absent.
In Java, Optional can help express values that may or may not exist:
These features are not replacements for reasoning about null, but they often make intent clearer.
Common Pitfalls
The most common mistake is treating empty values as null values. An empty string is not the same as null, and 0 is not the same as a missing number. If your logic merges them together, subtle bugs follow.
Another mistake is using equality when identity is the idiomatic rule, especially in Python. value is None communicates intent more clearly than value == None.
A third issue is forgetting that SQL uses different rules. WHERE column = NULL does not filter null rows correctly; use IS NULL.
In JavaScript, broad falsy checks also cause trouble. Code like if (!value) treats 0, false, "", null, undefined, and NaN the same way. That is fine only when you genuinely want all of them grouped together.
Finally, repeated null checks deep inside business logic usually signal weak data contracts. If a value may be absent, encode that clearly at the boundaries instead of scattering defensive checks everywhere.
Summary
- The proper null check depends on the language and on what "missing" means in context.
- Use
== nullin Java,is Nonein Python, and the appropriate strict or loose check in JavaScript. - In SQL, use
IS NULLandIS NOT NULL, not= NULL. - Distinguish
nullfrom empty strings, zero, false, and other non-null values. - Prefer early validation and clear API contracts over repeated ad hoc null checks.
Related reading
- What is the proper way to rethrow an exception in C?
- What is the real overhead of try/catch in C?
- What is the reason for Back-off restarting failed container for elasticsearch kubernetes pod?
- What is the use of assert in Python?
- What is Vim recording and how can it be disabled?
- What is wrong with this algorithm execution in Java?
- What kafka.common.OffsetOutOfRangeException means
- What killed my process and why?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.