You can simply use typeof. It will check undefined, null, 0 and "" also.

if(typeof RetailPrice!='undefined' && RetailPrice){
   return this.RetailPrice;
}
Answer from Parveen Sachdeva on Stack Overflow
🌐
SPGuides
spguides.com β€Ί check-if-string-is-empty-typescript
How to Check if a String is Empty in TypeScript?
March 26, 2025 - To check if a TypeScript string is null or empty, you can use the following two methods: This method combines checking for null and undefined with verifying if the string length is zero.
Top answer
1 of 16
5110

Empty string, undefined, null, ...

To check for a truthy value:

if (strValue) {
    // strValue was non-empty string, true, 42, Infinity, [], ...
}

To check for a falsy value:

if (!strValue) {
    // strValue was empty string, false, 0, null, undefined, ...
}

Empty string (only!)

To check for exactly an empty string, compare for strict equality against "" using the === operator:

if (strValue === "") {
    // strValue was empty string
}

To check for not an empty string strictly, use the !== operator:

if (strValue !== "") {
    // strValue was not an empty string
}
2 of 16
1446

For checking if a variable is falsey or if it has length attribute equal to zero (which for a string, means it is empty), I use:

function isEmpty(str) {
    return (!str || str.length === 0 );
}

(Note that strings aren't the only variables with a length attribute, arrays have them as well, for example.)

Alternativaly, you can use the (not so) newly optional chaining and arrow functions to simplify:

const isEmpty = (str) => (!str?.length);

It will check the length, returning undefined in case of a nullish value, without throwing an error. In the case of an empty value, zero is falsy and the result is still valid.

For checking if a variable is falsey or if the string only contains whitespace or is empty, I use:

function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}

If you want, you can monkey-patch the String prototype like this:

String.prototype.isEmpty = function() {
    // This doesn't work the same way as the isEmpty function used 
    // in the first example, it will return true for strings containing only whitespace
    return (this.length === 0 || !this.trim());
};
console.log("example".isEmpty());

Note that monkey-patching built-in types are controversial, as it can break code that depends on the existing structure of built-in types, for whatever reason.

🌐
Omarileon
omarileon.me β€Ί blog β€Ί typescript-null-undefined
mari. | How to Detect Null and Undefined in Your TypeScript Code
February 27, 2024 - The key here though is that anything ...ndefined!'); //Prints Β· Another way to check for null or undefined is to use the nullish coalescing operator (??), which was introduced in TypeScript 3.7....
🌐
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.
🌐
Sentry
sentry.io β€Ί sentry answers β€Ί javascript β€Ί how do i check for an empty/undefined/null string in javascript?
How do I Check for an Empty/Undefined/Null String in JavaScript? | Sentry
To check that a value is not an empty string, null, or undefined, you can create a custom function that returns true if a value is null, undefined, or an empty string and false for all other falsy values and truthy values:
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί typescript β€Ί how-to-check-null-and-undefined-in-typescript
How to check null and undefined in TypeScript ? - GeeksforGeeks
July 23, 2025 - These operators provide a concise and readable way to handle null and undefined values in TypeScript. Example: Below is an example demonstrating the use of optional chaining and nullish coalescing operators to check for null or undefined values. We declare variables that can be either undefined, null, or assigned a value. let undefinedVar: string | undefined; let nullVar: string | null = null; let assignedVar: string | null | undefined = "Hello, World!";
Find elsewhere
🌐
Shaikhu
shaikhu.com β€Ί how-to-check-if-a-string-is-null-blank-empty-or-undefined-using-javascript
How to check if a string is null, blank, empty or undefined using JavaScript? - shaikhu.com
Here we are using logical NOT operator to identify is a const variable str holds a string. ... 1const str1 = null 2const str2 = undefined 3 4if (!str1 || !str2){ 5 console.log("This is empty!, i.e. either null or undefined") 6} The above snippet will print "This is empty, i.e. either null or undefined". The above code works for both null,undefined and empty string like "".
🌐
Quora
quora.com β€Ί How-do-I-check-an-empty-undefined-null-string-in-JavaScript
How to check an empty/undefined/null string in JavaScript - Quora
Answer: If you want to check if the variable s contains an empty string or undefined or null, the easiest way would be to check if s is falsy using the not operator: [code]if (!s) { console.log("s is falsy"); } [/code]A problem is that this ...
🌐
Bobby Hadz
bobbyhadz.com β€Ί blog β€Ί typescript-check-if-null
How to correctly check for Null in TypeScript | bobbyhadz
Copied!type Person = { name: string | null; }; const person: Person = { name: null, }; if (typeof person.name === 'string') { // βœ… TypeScript knows person.name is string // πŸ‘‡οΈ (property) name: string console.log(person.name); console.log(person.name.toLowerCase()); } You can also check if a variable is null or undefined by using the optional chaining (?.) operator.
🌐
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.
🌐
Webdevtutor
webdevtutor.net β€Ί blog β€Ί typescript-check-if-is-null-or-empty-string
How to Check if a String is Null or Empty in Typescript
You can also create a utility function to check for null or empty strings, which can be reused throughout your codebase: function isNullOrEmpty(str: string | null): boolean { return !str || !str.trim(); } // Example usage const myString: string | null = ''; if (isNullOrEmpty(myString)) { console.log('String is null or empty'); } By implementing these methods, you can ensure that your Typescript code handles null or empty strings effectively, leading to more robust and reliable applications.
🌐
Medium
codehome.medium.com β€Ί how-do-i-check-for-an-empty-undefined-null-string-in-javascript-b4a9507742b4
How do I check for an empty/undefined/null string in JavaScript? | by Programise | Medium
January 16, 2023 - If a variable is undefined, typeof will return "undefined". let variable; if(typeof variable === "undefined") { console.log("Variable is undefined"); } 3. To check for a null value, you can use the comparison operator == or ===. Both null and ...
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί javascript-check-empty-string-checking-null-or-empty-in-js
JavaScript Check Empty String – Checking Null or Empty in JS
November 7, 2024 - Before we begin, you need to understand what the terms Null and Empty mean, and understand that they are not synonymous. For example, if we declare a variable and assign it an empty string, and then declare another variable and assign it the Null value, we can tell them apart by looking at their datatype:
🌐
W3docs
w3docs.com β€Ί javascript
How to Check for Empty/Undefined/Null String in JavaScript
let undefinedStr; if (!undefinedStr) ... null or undefined, and you intend to check for an empty one, you can use the length property of the string prototype, as follows:...
🌐
Bobby Hadz
bobbyhadz.com β€Ί blog β€Ί javascript-check-if-string-is-empty
How to check if a String is Empty in JavaScript | bobbyhadz
Use the `length` property on the string to check if it is empty. If the string's length is equal to `0`, then it's empty, otherwise, it isn't empty.
🌐
Webdevtutor
webdevtutor.net β€Ί blog β€Ί typescript-string-isnullorempty
Detecting Null or Empty Strings in TypeScript - A Complete Guide
When working with strings in TypeScript, it's essential to know how to detect whether a string is null or empty. In this guide, we'll explore various methods for achieving this, including the use of the nullish coalescing operator (??) and the strict equality operator (===).