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
๐ŸŒ
W3Schools
w3schools.com โ€บ typescript โ€บ typescript_null.php
TypeScript Null & Undefined
Nullish coalescing is another JavaScript feature that also works well with TypeScript's null handling. It allows writing expressions that have a fallback specifically when dealing with null or undefined. This is useful when other falsy values can occur in the expression but are still valid. It can be used with the ?? operator in an expression, similar to using the && operator. function printMileage(mileage: number | null | undefined) { console.log(`Mileage: ${mileage ??
๐ŸŒ
Omarileon
omarileon.me โ€บ blog โ€บ typescript-null-undefined
mari. | How to Detect Null and Undefined in Your TypeScript Code
February 27, 2024 - The most straightforward way of checking is of course the equality operator. const myValue = maybeNullOrUndefined(); if (myValue === null) console.log('Null!'); if (myValue === undefined) console.log('Undefined!');
๐ŸŒ
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. Previously, the type analysis performed for type guards was limited to if statements and ?: conditional expressions and didnโ€™t include effects of assignments and control flow constructs such as return and break statements.
๐ŸŒ
Rampatra
blog.rampatra.com โ€บ null-vs-undefined-in-typescript-or-javascript-how-to-check-for-both-at-once
!== null vs !== undefined in Typescript or Javascript, how to check for both at once?
Youโ€™re dealing with cases where a variable might be uninitialized or omitted. Checking whether a parameter was passed to a function. ... Checking against a value explicitly set as null. ... You want to check for both null and undefined together, since null and undefined are considered equal in loose equality (==). Commonly used when you donโ€™t care about the specific type of โ€œempty.โ€ ยท if (value != null) { // Do something if value is not null or undefined }
๐ŸŒ
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 - Null might mean the search has been completed, but the data wasnโ€™t found, whereas undefined might indicate that the fetch was never completed successfully. Another way to check for null or undefined is to use the nullish coalescing operator (??), ...
๐ŸŒ
GitBook
basarat.gitbook.io โ€บ typescript โ€บ recap โ€บ null-undefined
Null vs. Undefined | TypeScript Deep Dive
Recommend == null to check for both undefined or null. You generally don't want to make a distinction between the two. ... function foo(arg: string | null | undefined) { if (arg != null) { // arg must be a string as `!=` rules out both null ...
Find elsewhere
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ typescript-check-if-null
How to correctly check for Null in TypeScript | bobbyhadz
The first if statement uses the loose equals (==) operator instead of strict equals (===), and checks if the variable is equal to null or undefined. This checks for both null and undefined because when using loose equals (==), null is equal ...
๐ŸŒ
Angular Wiki
angularjswiki.com โ€บ angular โ€บ how-to-check-both-null-or-undefined-in-typescript-or-angular
How To Check Both null or undefined in TypeScript/Angular | Angular Wiki
different ways we can check both null or undefined in TypeScript or Angular. 1. By using simple if condition 2. By using TypeScript Nullish Coalescing & Optional chaining. 3. By using Array.include() function.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javascript-typescript-object-null-check
JavaScript/ Typescript object null check?
In this example below, we've assigned NULL to the variable and checked whether object is NULL or not with strict equality operator (===). ... We need to run the above code in node.js terminal to get the output. var value=null; if(!value) { console.log("This is null."); } else { console.log("This is not null."); }
๐ŸŒ
Tektutorialshub
tektutorialshub.com โ€บ home โ€บ typescript โ€บ null vs undefined in typescript
Null Vs Undefined in TypeScript - Tektutorialshub
March 15, 2023 - But they are neither false nor true. You can use typeof operator to check for undefined but not null as it returns โ€œobjectโ€. You can use the == & === operator to check their values
๐ŸŒ
Reddit
reddit.com โ€บ r/typescript โ€บ undefined vs null
r/typescript on Reddit: Undefined vs null
February 27, 2023 -

Since switching to TypeScript I have been using a lot of optional properties, for example:

type store = {
  currentUserId?: string
}

function logout () {
  store.currentUserId = undefined
}

However my coworkers and I have been discussing whether null is a more appropriate type instead of undefined, like this:

type store = {
  currentUserId: string | null
}

function logout () {
  store.currentUserId = null
}

It seems like the use of undefined in TypeScript differs slightly from in Javascript.

Do you guys/girls use undefined or null more often? And, which of the examples above do you think is better?

๐ŸŒ
Zipy
zipy.ai โ€บ blog โ€บ debug-typescript-null-or-undefined-value-errors
Solving Typescript Null & Undefined Errors: Expert Tips
February 28, 2024 - This design choice is inherited from JavaScript, providing flexibility in indicating the absence of a value (null) versus an uninitialized variable (undefined). ... Zipy offers proactive error monitoring and session replay capabilities, allowing developers to see exactly what led to an error, including the user's actions and the application state at the time of the error. Understand TypeScript's Type System: Leveraging TypeScript's strict typing can prevent many Null or Undefined Value Errors.
๐ŸŒ
Amit Merchant
amitmerchant.com โ€บ strictly-check-null-undefined-typescript
Strictly check for null and undefined values in TypeScript
May 27, 2020 - But TypeScript doesnโ€™t warn you when you assign null and undefined to the variables of common types. For instance, take the following example. function appendDomain(url: string) { return url.concat('.com'); } console.log(appendDomain('amitmerchant')); Iโ€™ve written this simple function in TypeScript that accepts its only parameter url which is of type string. Now, this will work fine if you compile it down to the JavaScript file providing I have the following tsconfig.json file setup.
๐ŸŒ
Honlsoft
honlsoft.com โ€บ blog โ€บ 2021-07-20-typescript-tips-null-coalescing
Typescript Tips: null(and undefined) Checking | Honlsoft
The huge difference here is it is just checking for null or undefined. In the past the || operator was commonly used. However, that worked differently on falsy values. So, the "" and 0 case behave differently as they are considered falsy. ...
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ where-null-and-undefined-are-watched-in-typescript-647ecabe346f
Where Null and Undefined Are Watched in TypeScript | Medium
July 1, 2025 - The compiler flag strictNullChecks tells TypeScript whether to be lenient or exact when it comes to null and undefined. If this setting is turned off, any type can be treated like it might be missing something.
๐ŸŒ
Reddit
reddit.com โ€บ r/typescript โ€บ how check for undefined in conditional type
r/typescript on Reddit: How check for undefined in conditional type
November 14, 2023 -

This code do not work at all, but not sure why.

type IsUndefined<T> = T extends undefined ? 1 : 0;

For example:

type IsTypeUndefined = IsUndefined<number | undefined>;
// This returns: 1 | 0

Is there a way for check if a type is undefined in a conditional?