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
TypeScript has a powerful system to deal with null or undefined values. By default null and undefined handling is disabled, and can be enabled by setting strictNullChecks to true. The rest of this page applies for when strictNullChecks is enabled.
๐ŸŒ
TypeScript
typescriptlang.org โ€บ docs โ€บ handbook โ€บ release-notes โ€บ typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
Non-null and non-undefined type guards may use the ==, !=, ===, or !== operator to compare to null or undefined, as in x != null or x === undefined. The effects on subject variable types accurately reflect JavaScript semantics (e.g. double-equals operators check for both values no matter which ...
๐ŸŒ
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.
๐ŸŒ
GitBook
basarat.gitbook.io โ€บ typescript โ€บ recap โ€บ null-undefined
Null vs. Undefined | TypeScript Deep Dive
So to check if a variable is defined or not at a global level you normally use typeof: ... if (typeof someglobal !== 'undefined') { // someglobal is now safe to use console.log(someglobal); } Because TypeScript gives you the opportunity to document your structures separately from values instead of stuff like: ... Node style callback functions (e.g. (err,somethingElse)=>{ /* something */ }) are generally called with err set to null if there isn't an error.
๐ŸŒ
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?
if (value != null) { // Do something if value is not null or undefined } Prefer strict equality checks (!== or ===) for better type safety and clarity.
๐ŸŒ
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?

Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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 ยท Checking for null.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javascript-typescript-object-null-check
JavaScript/ Typescript object null check?
In typescript, unassigned values are by default undefined, so in order to make a variable null, we must assign it a null value. To check a variable is null or not in typescript we can use typeof or "===" operator.
๐ŸŒ
Zipy
zipy.ai โ€บ blog โ€บ debug-typescript-null-or-undefined-value-errors
Solving Typescript Null & Undefined Errors: Expert Tips
February 28, 2024 - Efficiently debug & fix TypeScript null or undefined value errors. Expert tips for handling & resolving errors in TypeScript.
๐ŸŒ
GitHub
yellowduck.be โ€บ posts โ€บ null-vs-undefined-in-typescript-and-javascript-whats-the-difference
๐Ÿฅ Null vs. Undefined in TypeScript and JavaScript: what's the difference?
In TypeScript, the handling of null and undefined can be stricter with strictNullChecks. When enabled, variables cannot be null or undefined unless explicitly allowed. ... This condition is true for null, undefined, 0, NaN, "" (empty string), ...
๐ŸŒ
Honlsoft
honlsoft.com โ€บ blog โ€บ 2021-07-20-typescript-tips-null-coalescing
Typescript Tips: null(and undefined) Checking | Honlsoft
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.
๐ŸŒ
Webdevtutor
webdevtutor.net โ€บ blog โ€บ typescript-check-if-value-is-not-undefined-or-null
How to Check if a Value is Not Undefined or Null in TypeScript
You can create a type guard to check if a value is not undefined or null like this: function isDefined<T>(value: T | undefined | null): value is T { return value !== undefined && value !== null; } let exampleValue: string | undefined = "Hello, TypeScript!"; if (isDefined(exampleValue)) { ...
๐ŸŒ
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 ...