How Can I Determine If A String Is Empty, Undefined, Or Null In JavaScript?

To check if a string is empty, undefined, or null in JavaScript, you can employ the following methods:

  1. Using the length property:
let str = ""; // empty string

if (str.length === 0) {
    console.log("String is empty");
  1. Using strict equality (===) and a null check:
let str = null; // null value

if (str === "" || str === undefined || str === null) {
    console.log("String is empty, undefined, or null");
  1. Using the typeof operator:
let str; // undefined value

if (typeof str === "undefined" || str === null || str === "") {
    console.log("String is empty, undefined, or null");

It is important to handle each case separately as the conditions for an empty string, undefined, and null are not equivalent.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!