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 ??
🌐
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.
🌐
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!');
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › javascript-typescript-object-null-check
JavaScript/ Typescript object null check?
In this example below, we've assigned ... 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."); }...
🌐
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 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 }
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-check-if-null
How to correctly check for Null in TypeScript | bobbyhadz
To check for null in TypeScript, use a comparison to check if the value is equal or is not equal to `null`.
Find elsewhere
🌐
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?

🌐
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 (??), ...
🌐
Sandro Maglione
sandromaglione.com › articles › software › isnotnullable - check if a variable is not nullable in typescript
isNotNullable - Check if a variable is not nullable in Typescript | Sandro Maglione
July 24, 2022 - const isNotNullable = <T>(element: T | null | undefined): element is T => typeof element !== "undefined" && element !== null; const func = (item: { name: string } | null | undefined) => { if (isNotNullable(item)) { // Here `item` is of type `{ name: string }`, guaranteed! return item.name; } return "Nullable!"; } console.log(func({ name: 'Typescript' })); // Typescript console.log(func(null)); // Nullable!
🌐
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 ...
🌐
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.
🌐
Scaler
scaler.com › topics › typescript › strictnullchecks
StrictNullChecks in TypeScript - Scaler Topics
February 8, 2023 - TypeScript 2.0 introduces this option. We activate it by adding strictNullChecks: true to the tsconfig.json file, which assures that the compiler will throw an error if we assign Null and Undefined to other types.
🌐
Honlsoft
honlsoft.com › blog › 2021-07-20-typescript-tips-null-coalescing
Typescript Tips: null(and undefined) Checking | Honlsoft
console.log(null || "hello") // Outputs: hello console.log(undefined || "hello") // Outputs: hello console.log("world" || "hello") // Outputs: world console.log("" || "hello") // Outputs: hello console.log(0 || "hello") // Outputs: hello · If you've been using Typescript for some time you'll likely have come across both of these ways to deal with nulls.
🌐
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
🌐
TypeScript
typescriptlang.org › tsconfig › strictNullChecks.html
TypeScript: TSConfig Option: strictNullChecks
When strictNullChecks is false, null and undefined are effectively ignored by the language.
🌐
Gitbooks
hamednourhani.gitbooks.io › typescript-book › content › docs › tips › null.html
null is bad · typescript-book
When creating your own APIs its okay to use null in this case for consistency. In all sincerity for your own APIs you should look at promises, in that case you actually don't need to bother with absent error values (you handle them with .then vs. .catch). ... function toInt(str: string): { valid: boolean, int?: number } { const int = parseInt(str); if (isNaN(int)) { return { valid: false }; } else { return { valid: true, int }; } }