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
let value: string | undefined | null = null; value = 'hello'; value = undefined; Try it Yourself » · When strictNullChecks is enabled, TypeScript requires values to be set unless undefined is explicitly added to the type. Optional chaining is a JavaScript feature that works well with TypeScript's null handling.
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. Specifically, the operation x! produces a value of the type of x with null and undefined excluded. Similar to type assertions of the forms <T>x and x as T, the ! non-null assertion operator is simply removed in the emitted JavaScript code.
🌐
TutorialsPoint
tutorialspoint.com › javascript-typescript-object-null-check
JavaScript/ Typescript object null check?
To check a variable is null or not in typescript we can use typeof or "===" operator. The typeof operator in JavaScript is used to find the datatype of the variable.
🌐
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. By using typescript compiler tcs we transpile typescript code to javascript and then run the javascript file.
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-check-if-null
How to correctly check for Null in TypeScript | bobbyhadz
The type of null is "object" in JavaScript, so you shouldn't use the typeof operator to check for null. ... Copied!type Person = string | null; const person: Person = null; console.log(typeof person); // 👉️ "object" ...
🌐
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.
Find elsewhere
🌐
GitBook
basarat.gitbook.io › typescript › recap › null-undefined
Null vs. Undefined | TypeScript Deep Dive
Interestingly in JavaScript with ==, null and undefined are only equal to each other: ... // Both null and undefined are only `==` to themselves and each other: console.log(null == null); // true (of course) console.log(undefined == undefined); // true (of course) console.log(null == undefined); // true // You don't have to worry about falsy values making through this check console.log(0 == undefined); // false console.log('' == undefined); // false console.log(false == undefined); // false
🌐
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?
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 javascript ...
🌐
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 12, 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?

🌐
Scaler
scaler.com › topics › typescript › strictnullchecks
StrictNullChecks in TypeScript - Scaler Topics
February 8, 2023 - The easiest approach to check for ... distinguish null from the other false values. In JavaScript, use the equality operators == or === to check for a null value....
🌐
Honlsoft
honlsoft.com › blog › 2021-07-20-typescript-tips-null-coalescing
Typescript Tips: null(and undefined) Checking | Honlsoft
The Elvis operator is a shorthand way to check for null or undefined without requiring a separate if statement. It looks something like this in practice. console.log(obj?.hello) // logs null if the obj is null, and does not reference hello · Well this is a simple case, some more uncommon ones I don't often see samples for are arrays and function calls. What if I want to access an object as an array, but it could be null or undefined?
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-3-7.html
TypeScript: Documentation - TypeScript 3.7
For more details, check out their pull request and the nullish coalescing proposal repository. ... There’s a specific set of functions that throw an error if something unexpected happened. They’re called “assertion” functions. As an example, Node.js has a dedicated function for this called assert. ... In this example if someValue isn’t equal to 42, then assert will throw an AssertionError. Assertions in JavaScript ...
🌐
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
There are different ways we can check both null or undefined in TypeScript or Angular. By using simple if condition. By using TypeScript Nullish Coalescing & Optional chaining. By using Array.include() function. TypeScript is super set of JavaScript.
🌐
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.
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › null in typescript
Null in TypeScript - Tektutorialshub
March 15, 2023 - Although the data type of null ... bug is not fixed as it will break many existing codes. You cannot use typeof operator to check for null as it returns “object”. Hence you can either use the == or ===...
🌐
Zipy
zipy.ai › blog › debug-typescript-null-or-undefined-value-errors
Solving Typescript Null & Undefined Errors: Expert Tips
February 28, 2024 - TypeScript treats them differently in strict mode. Can optional chaining help with Null or Undefined Value Errors? Yes, optional chaining (?.) allows you to safely access deeply nested properties without having to explicitly check for null or undefined at each level. ... This design choice is inherited from JavaScript, providing flexibility in indicating the absence of a value (null) versus an uninitialized variable (undefined).
🌐
Shinesolutions
shinesolutions.com › home › writing safer code with typescript strict null checks & type guards
Writing safer code with TypeScript strict null checks & type guards - Shine Solutions Group
January 6, 2017 - In this article I will show you how to write safer TypeScript code by leveraging a feature called strictNullChecks. It’s quite easy to miss because it’s turned off by default, but can help a lot to produce more robust code. I’ll also briefly introduce another lesser-known feature of the language called type guards. Some Java full-stack developers (like me) always wanted to have statically typed JavaScript.
🌐
Gitbooks
hamednourhani.gitbooks.io › typescript-book › content › docs › tips › null.html
null is bad · typescript-book
If your code base interacts with other APIs that might give you a null you check with == undefined (instead of ===). Using this is safe even for other potentially falsy values. /// Image you are doing `foo == undefined` where foo can be one of: console.log(undefined == undefined); // true ...