If you want to check if a variable (say v) has been defined and is not null:
if (typeof v !== 'undefined' && v !== null) {
// Do some operation
}
If you want to check for all falsy values such as: undefined
, null
, ''
, 0
, false
:
if (v) {
// Do some operation
}