Categories
comparison javascript null undefined

Is there a standard function to check for null, undefined, or blank variables in JavaScript?

2854

Is there a universal JavaScript function that checks that a variable has a value and ensures that it’s not undefined or null? I’ve got this code, but I’m not sure if it covers all cases:

function isEmpty(val){
    return (val === undefined || val == null || val.length <= 0) ? true : false;
}

13

  • 6

    possible duplicate of How do you check for an empty string in JavaScript?

    Jun 10, 2013 at 19:40

  • 157

    Protip, never do (truthy statement) ? true : false;. Just do (truthy statement);.

    Aug 2, 2017 at 17:55


  • 8

    @GeorgeJempty not a dup, since the other answer asks about strings in particular, whereas this one asks about variables.

    – Madbreaks

    Feb 1, 2018 at 0:55

  • 3

    Any correct answer to this question relies entirely on how you define “blank”.

    – Madbreaks

    Feb 1, 2018 at 1:03

  • 4

    @Jay It doesn’t hurt anything as far as execution of your code. It’s just overly verbose. You wouldn’t say, “Is are you hungry is true?” You just “Are you hungry” So in code just say if (hungry) … instead of if (hungry === true) …. Like all coding things in this manner, it’s just a matter of taste. More specific to the example provided by the OP he’s saying even more verbosely, “If it’s true, then true, if not then false” But if it’s true, then it’s already true. And, if it’s false, it’s already false. This is akin to saying “If you’re hungry then you are, and if not then you aren’t.”

    Jan 30, 2019 at 22:58

5408

You can just check if the variable has a truthy value or not. That means

if( value ) {
}

will evaluate to true if value is not:

  • null
  • undefined
  • NaN
  • empty string (“”)
  • 0
  • false

The above list represents all possible falsy values in ECMA-/Javascript. Find it in the specification at the ToBoolean section.

Furthermore, if you do not know whether a variable exists (that means, if it was declared) you should check with the typeof operator. For instance

if( typeof foo !== 'undefined' ) {
    // foo could get resolved and it's defined
}

If you can be sure that a variable is declared at least, you should directly check if it has a truthy value like shown above.

36

  • 162

    What if the value is a false boolean that was intended. Sometimes you want to give a default value if there is no value, which won’t work if false boolean was passed in.

    – TruMan1

    Dec 30, 2012 at 22:38


  • 118

    @TruMan1: in such a case (where your logic dictates the validation) you have to go like if( value || value === false ). Same goes for all falsy values, we need to validate for those explicitly.

    – jAndy

    Dec 31, 2012 at 4:58


  • 36

    Except if value is an array. The interpretation of truthy could be misleading. In that case we should be checking value.length != 0 for a non-empty array.

    – user

    Apr 18, 2014 at 21:06


  • 17

    Just want to add that if you feel the if construct is syntactically too heavy, you could use the ternary operator, like so: var result = undefined ? "truthy" : "falsy". Or if you just want to coerce to a boolean value, use the !! operator, e.g. !!1 // true, !!null // false.

    – KFL

    Aug 26, 2014 at 7:24


  • 9

    Also note that this will not check for strings which only contain whitespace characters.

    Nov 21, 2014 at 10:28

307

The verbose method to check if value is undefined or null is:

return value === undefined || value === null;

You can also use the == operator but this expects one to know all the rules:

return value == null; // also returns true if value is undefined

8

  • 41

    Checking for only null or undefined can be done like so: if (value == null). Mind the == operator that coerces. If you check like this if (value === null || value === undefined), you forgot/don’t know how Javascript coerces. webreflection.blogspot.nl/2010/10/…

    Jul 3, 2014 at 11:46


  • 44

    @ChristiaanWesterbeek: your point that arg == null produces same results as arg === undefined || arg === null. However, I consider the latter example more readable.

    – Salman A

    Jul 7, 2014 at 4:02

  • 15

    arg == null is pretty common in my experience.

    Oct 10, 2014 at 1:56

  • 9

    return value === (void 0) is safer than testing against undefined which may well be a legitimate variable in scope, sadly.

    – x0n

    Jan 19, 2016 at 5:09

  • 6

    @Sharky There’s a difference between a variable that is undefined and an undeclared variable: lucybain.com/blog/2014/null-undefined-undeclared

    Oct 17, 2016 at 10:22

113

function isEmpty(value){
  return (value == null || value.length === 0);
}

This will return true for

undefined  // Because undefined == null

null

[]

""

and zero argument functions since a function’s length is the number of declared parameters it takes.

To disallow the latter category, you might want to just check for blank strings

function isEmpty(value){
  return (value == null || value === '');
}

2

  • 11

    undefined == null but undefined !== null

    – Ian Boyd

    Feb 22, 2014 at 15:06

  • 3

    @IanBoyd that is because you are comparing == to ===. this means that undefined == null (true) undefined != null (false) undefined === null (false) undefined !== null(true) would be better to give a bit more information in order to be helpful and push people in the right direction. moz doc on the difference developer.mozilla.org/en-US/docs/Web/JavaScript/…

    Jan 14, 2016 at 18:03