Check empty/undefined/null string in JavaScript

To check if a JavaScript string is truthy and contains one or more characters, pass the string to an if statement.
if (strValue) { //strValue is NOT an empty string (""), undefined, null, 0, false, NaN }
There is not a standard function in JavaScript to check for empty string (""), undefined, null, 0, false or NaN values. However, there is the concept of truthy and falsy values in JavaScript. Values that coerce to true in conditional statements are called truth values and those that resolve to false are called falsy.

To check for a falsy value:

if (!strValue) { // strValue is an empty string (""), undefined, null, 0, false, NaN }

JavaScript === Operator

If you want to check for exactly an empty string, compare for strict equality against "" using the JavaScript "===" operator:
if (strValue === "") { // strValue is an empty string }

Using JavaScript length Property

You can check for the length of the string by adding its length property. If the length is equal to zero, it means that the string is empty else not empty.
const strValue=""; if(strValue.length === 0){ //strValue is an empty string } else{ // strValue is NOT an empty string }

Difference between empty, null, undefined and NaN


Javascript enpty String
An empty string ("") represents a string of zero length, while a null string shows that it does not contain any value. Moreover, the undefined term refers to it exists but hasn't been given a value yet and NaN is not a valid number . According to ES specification, the following values will evaluate to false in a conditional context -
  1. empty string ("")
  2. null
  3. undefined
  4. NaN
This means that none of the following if statements will get executed:
  1. if ("") {}
  2. if (null) {}
  3. if (undefined) {}
  4. if (NaN) {}