Using a juggling-check, you can test both null and undefined in one hit:

if (x == null) {

If you use a strict-check, it will only be true for values set to null and won't evaluate as true for undefined variables:

if (x === null) {

You can try this with various values using this example:

var a: number;
var b: number = null;

function check(x, name) {
    if (x == null) {
        console.log(name + ' == null');
    }

    if (x === null) {
        console.log(name + ' === null');
    }

    if (typeof x === 'undefined') {
        console.log(name + ' is undefined');
    }
}

check(a, 'a');
check(b, 'b');

Output

"a == null"

"a is undefined"

"b == null"

"b === null"

Answer from Fenton on Stack Overflow
🌐
Honlsoft
honlsoft.com › blog › 2021-07-20-typescript-tips-null-coalescing
Typescript Tips: null(and undefined) Checking | Honlsoft
July 20, 2021 - Thankfully, in Typescript, there are several shorthand ways to deal with nulls. The Elvis operator is a shorthand way to check for null or undefined without requiring a separate if statement.
Discussions

Is Null Checking Necessary in TypeScript for Runtime Safety?
Is your program going to work with a null value in that spot? <- focus on this More on reddit.com
🌐 r/reactjs
22
9
August 13, 2024
return null ?
For the first part: ngOnInit is not returning null, the arrow function passwordMatcher, which I assume is a form validator, is returning null. I personally prefer to put my validators in a separate function in a separate file, but both are fine. Angular form validators are expected to return null when no errors are found. Apart from that, the formControlNames might be a bit messed up newPassword => email and newPasswordRepeat => confirm. That looks like some ugly copy / paste. So something like: export function generateMatcherValidator(newControlKey: string, confirmControlKey: string) { return (control: AbstractControl): { [key: string]: boolean } => { const newControl = control.get(newControlKey); const confirmControl = control.get(confirmControlKey); if (!newControl || !confirmControl) { return null; } return newControl.value === confirmControl.value ? null : { nomatch: true }; }; } For the second part: I quite often just do a truthy check. Although I do try to keep my code as flat as possible, so instead of wrapping entire functions in an if-condition, I just return if that condition is false. So something like: function randomFunction(someArgumentUsedToCallAfunction) { if (!someArgumentUsedToCallAfunction) { return; } do stuff, because we were called with a value } More on reddit.com
🌐 r/typescript
10
5
December 20, 2020
How to access a possibly empty array by index?
if (matches == null). This will check for both null and undefined. Doing typeof matches === undefined doesn't really make sense. typeof returns a string, so you'd have to do typeof matches === 'undefined' if you wanted to use typeof. Nowadays it's possible to just test it against null or undefined directly, so there is no need to use typeof More on reddit.com
🌐 r/typescript
24
3
March 19, 2024
returning null vs throw error ? (+ some question about refactoring)
For the first one, depends. I prefer to throw, as it may not exist or a db reading error may happen, so throwing allows to describe what happened. Just a string as the error can do the job for simple applications, but may be a good idea to have an Error class/interface for it, so you can use error codes etc. There is the Left/Right pattern from the Functional Programming. I never used it and I personally think it's ugly, but you may like it, so do your research. More on reddit.com
🌐 r/typescript
12
7
February 22, 2022
People also ask

What is a null check?
In JavaScript, null represents an intentional absence of a value, indicating that a variable has been declared with a null value on purpose. On the other hand, undefined represents the absence of any object value that is unintentional. A null check determines whether a variable has a null value, meaning a valid instance of a type exists.
🌐
builtin.com
builtin.com › software-engineering-perspectives › javascript-null-check
How to Check for Null in JavaScript | Built In
What is a strict null check?
StrictNullChecks is a feature that treats null and undefined as two separate types, reducing errors and making it easier to find coding bugs. It also has stronger measures for defining variables as null or undefined, ensuring variables are declared as null only when it’s safe to do so.
🌐
builtin.com
builtin.com › software-engineering-perspectives › javascript-null-check
How to Check for Null in JavaScript | Built In
Is null false in JavaScript?
Null is not considered false in JavaScript, but it is considered falsy. This means that null is treated as if it’s false when viewed through boolean logic. However, this is not the same thing as saying null is false or untrue.
🌐
builtin.com
builtin.com › software-engineering-perspectives › javascript-null-check
How to Check for Null in JavaScript | Built In
🌐
TypeScript
typescriptlang.org › tsconfig › strictNullChecks.html
TypeScript: TSConfig Option: strictNullChecks
When strictNullChecks is true, null and undefined have their own distinct types and you’ll get a type error if you try to use them where a concrete value is expected. For example with this TypeScript code, users.find has no guarantee that it will actually find a user, but you can write code ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
August 26, 2025 - The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
TypeScript has a powerful system to deal with null or undefined values.
Find elsewhere
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-null-check
How to Check for Null in JavaScript | Built In
Summary: JavaScript offers several ways to check for null, including strict (===) and loose (==) equality, Object.is() and boolean coercion. Developers often use typeof and optional chaining (?.) to safely identify null, undefined or undeclared ...
Published   August 4, 2025
🌐
Upmostly
upmostly.com › home › typescript › how to detect null and undefined
How to Detect Null and Undefined in Your TypeScript Code - Upmostly
March 28, 2023 - Another way to check for null or undefined is to use the nullish coalescing operator (??), which was introduced in TypeScript 3.7.
🌐
Omarileon
omarileon.me › blog › typescript-null-undefined
mari. | How to Detect Null and Undefined in Your TypeScript Code
February 27, 2024 - Another way to check for null or undefined is to use the nullish coalescing operator (??), which was introduced in TypeScript 3.7. If the left-hand side of the operation is non-null it returns that, otherwise it returns the right-hand side otherwise.
🌐
C# Corner
c-sharpcorner.com › article › explain-null-handling-in-typescript
Explain Null Handling in TypeScript
March 21, 2024 - Null handling in TypeScript involves managing null and undefined values effectively to prevent runtime errors. Techniques like nullable types, optional chaining, type guards, and non-null assertion operators help ensure code reliability.
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
In practical terms, strict null checking mode requires that all files in a compilation are null- and undefined-aware. TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › narrowing.html
TypeScript: Documentation - Narrowing
In the printAll function, we try to check if strs is an object to see if it’s an array type (now might be a good time to reinforce that arrays are object types in JavaScript). But it turns out that in JavaScript, typeof null is actually "object"! This is one of those unfortunate accidents of history. Users with enough experience might not be surprised, but not everyone has run into this in JavaScript; luckily, TypeScript lets us know that strs was only narrowed down to string[] | null instead of just string[].
🌐
React
react.dev › learn › typescript
Using TypeScript – React
This causes the issue that you need to eliminate the | null in the type for context consumers. Our recommendation is to have the Hook do a runtime check for it’s existence and throw an error when not present:
🌐
Medium
medium.com › @seanbridger › typescript-basics-8-10-any-void-never-null-strict-null-checks-068ba946b08b
TypeScript Basics (8/10): Any, Void, Never, Null, Strict Null Checks | by Likely Coder | Medium
November 14, 2023 - let myString: string = null; // Error with strict null checks enabled · By default, TypeScript has a feature called “strict null checks” that enhances type safety by making it explicit when a variable can be null or undefined.
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-check-null-and-undefined-in-typescript
How to check null and undefined in TypeScript ? - GeeksforGeeks
July 23, 2025 - We can use typeof or '==' or '===' to check if a variable is null or undefined in typescript.
🌐
TypeScript
typescriptlang.org › docs › handbook › advanced-types.html
TypeScript: Documentation - Advanced Types
TypeScript has two special types, null and undefined, that have the values null and undefined respectively. We mentioned these briefly in the Basic Types section. By default, the type checker considers null and undefined assignable to anything. Effectively, null and undefined are valid values ...
🌐
Marius Schulz
mariusschulz.com › blog › nullish-coalescing-the-operator-in-typescript
Nullish Coalescing: The ?? Operator in TypeScript — Marius Schulz
August 14, 2021 - Assuming we're targeting "ES2019" or a lower language version, the TypeScript compiler will emit the following JavaScript code: value !== null && value !== void 0 ?
🌐
Sentry
sentry.io › sentry answers › javascript › how do i check for an empty/undefined/null string in javascript?
How do I Check for an Empty/Undefined/Null String in JavaScript? | Sentry
function isEmpty(value) { return ... console.log(isEmpty(undefined)); // true · The isEmpty function uses the equality operator (==) to check if the argument value is null or undefined....
🌐
Hacker News
news.ycombinator.com › item
Typescript is GREAT. Strict null-checking eliminates the dreaded null-pointer ex... | Hacker News
April 16, 2017 - The way I look at Typescript vs. Javascript is that with Typescript, it's like having always having a pair programming buddy (the compiler) who never makes mistakes · When I was using typescript this still came up if you missed an array bounds check, you'd end up with an unexpected undefined.
🌐
Reddit
reddit.com › r/reactjs › is null checking necessary in typescript for runtime safety?
r/reactjs on Reddit: Is Null Checking Necessary in TypeScript for Runtime Safety?
August 13, 2024 -

TypeScript types are only enforced at compile time and do not exist at runtime. Given this, if I define a TypeScript function that accepts a string parameter, is it necessary to perform null checks? This question arises because if the API returns null, it could lead to runtime errors.

Consider the following code snippet:

const capitalise = (val: string) => {
   // Question: Is this check redundant due to TypeScript's type checking?
   if (!val) {
      return '';
   }

   return val.toUpperCase();
};

// Assume 'name' is fetched from an API or another source that could return null
const name: string = /* fetch from API or another source */;

capitalise(name);

What are the best practices in such scenarios to ensure runtime safety in TypeScript applications?