最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to check falsy with undefined or null? - Stack Overflow

programmeradmin2浏览0评论

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' ?

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' ?

Share Improve this question asked Jul 14, 2014 at 5:44 Navin RauniyarNavin Rauniyar 10.5k14 gold badges46 silver badges70 bronze badges 1
  • 4 because false != null – yuvi Commented Jul 14, 2014 at 5:45
Add a comment  | 

4 Answers 4

Reset to default 10

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

You can check for falsy values using

var n = null;
if (!n) {
    console.log('null');
} else {
    console.log('has value');
}

Demo: Fiddle


Or check for truthiness like

var n = null;
if (n) { //true if n is truthy
    console.log('has value');
} else {
    console.log('null');
}

Demo: Fiddle

A value being "falsy" means that the result of converting it to a Boolean is false:

Boolean(null) // false
Boolean(undefined) // false
// but
Boolean("0") // true

This is very different from comparing it against a Boolean:

null == false // not equal, null == true is not equal either
undefined == false // not equal, undefined == true is not equal either
// but
"0" == true // not equal, however, `"0" == false` is equal

Since you are using strict comparison, the case is even simpler: the strict equality comparison operator returns false if operands are not of the same data type. null is of type Null and false is of type Boolean.

But even if you used loose comparison, the abstract equality algorithm defines that only null and undefined are equal to each other.


Depending on what exactly you want to test for, you have a couple of options:

if (!value)         // true for all falsy values

if (value == null)  // true for null and undefined

if (value === null) // true for null

In general you should always prefer strict comparison because JS' type conversion rules can be surprising. One of the exceptions is comparing a value against null, since loose comparison also catches undefined.

=== checks for identity - the exact same type and value. So null !== false firstly because they are not the same type, thus will not match when using ===.

If you just want to check for any falsey value, then check with:

if (!n)

If you want to specifically check for null, then check for null like this:

if (n === null)
发布评论

评论列表(0)

  1. 暂无评论