7

Although there are semantic differences between JavaScript's null and undefined, many times they can be treated as the same. What's the preferable way of checking if the value is either null or undefined?


Right now I'm doing the following:

if (typeof value === "undefined" || value === null) {
    // do something
}

Which is pretty verbose. I could, of course, create a function for this and import everywhere, but I'm wishing that there's a better way to achieve this.

Also, I know that

if (value == null) {
}

Will get the job done 90% of the time, unless value is zero... or false... or a number of implicit things that can cause obscure bugs.

Bruno Brant
  • 8,226
  • 7
  • 45
  • 90
  • if you want to test for specific values, you need verbosity ... although, you can shorten it a bit, `if (value === undefined || value === null) {` – Jaromanda X Jul 28 '16 at 23:03
  • You often don't need to do defensive null checks at all. But I can't answer your question more without seeing more of your code. Zoom out and show us the bigger picture. – Mulan Jul 28 '16 at 23:03
  • 1
    Possible duplicate of [How to determine if variable is 'undefined' or 'null'?](http://stackoverflow.com/questions/2647867/how-to-determine-if-variable-is-undefined-or-null) – Angel Politis Jul 28 '16 at 23:04
  • I think the commonly used one is the ``typeof a === "undefined"`` way – ismnoiet Jul 28 '16 at 23:05
  • @hamism: Um, except that will be false for `null`. – T.J. Crowder Jul 28 '16 at 23:17
  • TJ was right -- I ended up confusing comparison this with falsiness. I strikedout the part of the question that affirmed a very silly thing. Oh, @AngelPolitis, I didn't find that question. Indeed, it's a duplicate. – Bruno Brant Jul 28 '16 at 23:33
  • Yes @T.J.Crowder you're right. – ismnoiet Jul 29 '16 at 11:06

2 Answers2

29

Also, I know that

if (value == null) {
}

Will get the job done 90% of the time, unless value is zero... or false... or a number of implicit things that can cause obscure bugs.

No, it gets the job done 100% of the time. The only values that are == null are null and undefined. 0 == null is false. "" == undefined is false. false == null is false. Etc. You're confusing == null with falsiness, which is a very different thing.

That's not to say, though, that it's a good idea to write code expecting everyone to know that. You have a perfectly good, clear check in the code you're already using. Whether you choose to write value == null or the explicit one you're currently using (or if (value === undefined || value === null)) is a matter of style and in-house convention. But value == null does do what you've asked: Checks that value is null or undefined.

The details of == are here: Abstract Equality Comparison.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 2
    Thanks TJ, I was actually confusing things. – Bruno Brant Jul 28 '16 at 23:29
  • Although this is correct javascript a lint checker such as jslint or eslint may still complain about it, though most will have some sort of configuration setting to allow == for testing against null e.g. https://github.com/eslint/eslint/issues/910 – Glenn Lawrence Jun 07 '17 at 12:14
0

underscore js has a function for this _.isUndefined()

from https://underscorejs.org/#isUndefined

isUndefined _.isUndefined(value)
Returns true if value is undefined.

example:
_.isUndefined(window.missingVariable);
=> true

lodash has a similar function. see https://lodash.com/docs/4.17.11#isUndefined

Both have similar functions for isNull too.

I find the functions are useful for others to know what is being tested for.

Thaddeus Albers
  • 4,094
  • 5
  • 32
  • 42