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.
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
Object is possibly 'null' after null check.
const [map, setMap] = React.useState(null) ^ Here's the problem. Typescript is inferring the type of map here as null. i.e. it doesn't think map will every be anything other than null. You need to explicitly set map's type. Try something like this const [map, setMap] = React.useState(null) More on reddit.com
๐ŸŒ r/typescript
21
5
December 3, 2020
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
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
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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?

๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
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?
November 14, 2024 - Use != null only when you explicitly want to check for both null and undefined. ... If offerPrice might explicitly be null, you should use offerPrice != null to handle both cases. Take your presentation to the next level. ... Put your face and name on your screen. ... Show keypresses on your screen. ... Your to-dos on your menu bar. ... Fill forms using your right-click menu. ... Preview your Mac app icons. ... question typescript ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javascript-typescript-object-null-check
JavaScript/ Typescript object null check?
June 18, 2024 - A variable is undefined until and ... 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....
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 โ€บ 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: