how to check falsy with undefined or null?

javascript, jquery

Solution

To solve your problem:

You can use not operator(!):

var n = null;
if(!n){ //if n is undefined, null or false
console.log('null');
} else{
console.log('has value');
}
// logs null

To answer your question:

It is considered falsy or truthy for Boolean. So if you use like this:

var n = Boolean(null);
if(n===false){
console.log('null');
} else{
console.log('has value');
}
//you'll be logged null

Problem

undefined and null are falsy in javascript but, ``` var n = null; if(n===false){ console.log('null'); } else{ console.log('has value'); } ``` but it returns 'has value' when tried in console, why not 'null' ?

Original source