admin管理员组

文章数量:1278918

I want to check the null value in JavaScript.

For instance, test() is function which could return null. So If I want to test the null check efficiently what should be my approach.

var a = test();

followed by

if (a) {}

OR

if (a !== null) {}

? since if (a) will check for null, undefined, false, 0, NaN which might not be the best approach when we know we could get only null value.

I want to check the null value in JavaScript.

For instance, test() is function which could return null. So If I want to test the null check efficiently what should be my approach.

var a = test();

followed by

if (a) {}

OR

if (a !== null) {}

? since if (a) will check for null, undefined, false, 0, NaN which might not be the best approach when we know we could get only null value.

Share Improve this question edited Apr 16, 2014 at 6:15 MaVRoSCy 17.8k15 gold badges85 silver badges128 bronze badges asked Apr 16, 2014 at 6:14 djadmindjadmin 1,7603 gold badges19 silver badges27 bronze badges 5
  • If I want to test the null check efficiently, is that the bottleneck in your application? – thefourtheye Commented Apr 16, 2014 at 6:15
  • I want to check the null value. So check the null value: if (a !== null) {}? – dfsq Commented Apr 16, 2014 at 6:17
  • 1 if you don't check for strict equality with the value null, you will be checking for all false-y values. so you must use 'if (a === null){...}'. FYI, null is not the same as undefined (or 0, or false, or ""). – adrichman Commented Apr 16, 2014 at 6:17
  • Yeah, my question is basically about the efficiency, whether using a !== null ,a better approach ? or it doesn't matter at all. – djadmin Commented Apr 16, 2014 at 6:18
  • Many years late but @adrichman's ment above is misleading. You do not need a strict equality; loose equality would be sufficient unless you specifically want to rule out undefined. false == null, 0 == null, -0 == null, 0n == null, NaN == null, "" == null are all false. undefined == null and null == null are true. – tremby Commented Mar 2, 2023 at 17:43
Add a ment  | 

2 Answers 2

Reset to default 5

To check null only

if (value !== null) {
}

If you want to check even for 'undefined'

if (typeof value !== 'undefined' && value !== null)

I will remend you to check both undefined and null value.

function test() {
  var a;
  return a;  // returns undefined
}

function test() {
  var a = null;
  return a;  // returns null   
}

So there is a possibility that value can undefined or null. So better to check both.

As you pointed out, different methods check for different things and you need to choose the most suitable one depending on your needs. The differences in performance between them are minuscule and certainly not worth micro-optimizing against.


if (a)

Ensures a truthy value. This might not be what you wanted if a return value of 0 or '' is actually valid.


if (a !== null)

Ensures a null returned value is filtered.


if (a !== null && a !== undefined)

Ensures a null or undefined value is filtered.


if (a != null)

Same as the previous one, filters out null and undefined values. It is shorter, but certain coding styles enforce strict equality checks and a careless maintainer could inadvertendly change the logic by "fixing" it.


var a = test() || default;

Not exactly a check, but very useful because it doesn't branch your logic. When a certain default value can be applied if the result of the test() call is falsy, you might want to use this to avoid the if alltogether and have a clear code path.

本文标签: Javascript best practice to check only null valuesStack Overflow