How To Check A Non-Empty Object Or String in JavaScript

In this tutorial i will discuss how to check a non-empty object or string in javascript.There are several condition that can be considered empty in javascript.The empty value may be a non-numeric value in a number, an invalid date, and empty string, an undefined value, and a null value.
Below is simple method to check for a non-empty object or string.

Step 1: First we check an object
if (typeof obj == 'undefined' || obj === null || obj === '') return true;
if object is type of object then we will validate object equal to undefined ,object equal to null or equal to empty string that’s mean object is empty.

Step 2: it’s a datetype of variable
if (obj instanceof Date && isNaN(Number(obj))) return true;

If it’s a date, and Number(obj) isNaN, then it’s an invalid date, and its Empty.

Step 3: it’s a number type of variable
if (typeof obj == 'number' && isNaN(obj)) return true;

if its number then we will check number is isNaN (Not a Number), that’s mean it’s empty.

[code type=”javascript”]
function isEmpty(obj) {
if (typeof obj == ‘undefined’ || obj === null || obj === ”) return true;
if (obj instanceof Date && isNaN(Number(obj))) return true;
if (typeof obj == ‘number’ && isNaN(obj)) return true;
return false;
}