Building on that, I totally agree with @ishrth_fathima about using val == null for the javascript check null or undefined. In addition, trimming strings is key to catching those sneaky cases where the string might look empty but actually has spaces. One thing to keep in mind is the order of checks to avoid errors with types that don’t have a .length property. Here’s a slightly tweaked version I find even safer:
function isEmpty(val) {
return val == null || (typeof val === 'string' && !val.trim()) || (Array.isArray(val) && val.length === 0);
}
By explicitly checking if val is an array before checking .length, you reduce unexpected errors. This way, your javascript check null or undefined and blank cases handle strings with spaces, arrays, and those nullish values gracefully."