Is there a universal JavaScript function that checks that a variable has a value and ensures that it\'s not undefined
or null
? I\'ve got this code,
! check for empty strings (""), null, undefined, false and the number 0 and NaN. Say, if a string is empty var name = ""
then console.log(!name)
returns true
.
function isEmpty(val){
return !val;
}
this function will return true if val is empty, null, undefined, false, the number 0 or NaN.
OR
According to your problem domain you can just use like !val
or !!val
.