5

I like to use the !! in Javascript to ensure that a variable is set and that no error will be thrown.

However today I have a variable with 0 value that is valid for me. I need to ensure that it is not NaN nor undefined, is there really no short way to do it without the boring if (variable !== NaN && variable !== undefined?

If that may help, I am using angular.

Thx.

Rolintocour
  • 2,934
  • 4
  • 32
  • 63
  • undefined and NaN are both falsy values, so you can use `if (variable) { }` – Kuba Janik Mar 30 '20 at 08:03
  • 1
    Does this answer your question? [Is there a way to check for both \`null\` and \`undefined\`?](https://stackoverflow.com/questions/28975896/is-there-a-way-to-check-for-both-null-and-undefined) – Mehrdad Mar 30 '20 at 08:05
  • @JakubJanik, it will return false for 0 also, which is not desired here. – uiTeam324 Mar 30 '20 at 08:09
  • 1
    FYI, `variable !== NaN` will **always** be true, because `NaN != NaN`. To properly check for `NaN` you need to use `isNaN()`. – Lennholm Mar 30 '20 at 08:49
  • Based on the already provided answers, you could use `if (variable != null && !isNaN(variable))`. As far as I know, this will be the shortest way to implement your logic. – Bart Hofland Mar 30 '20 at 08:52
  • I would also watch for the `undefined` value with a: `if (variable != undefined && variable != null && !isNaN(variable))` – Stephane Nov 28 '20 at 09:59
  • Depending on its content a non empty array can still be a `NaN` value. To guard against this: `&& (Array.isArray(value) || !isNaN(value)` – Stephane Nov 28 '20 at 15:06

3 Answers3

5
let a = 'value';
if (isNaN(a) || a == null) {
    console.log('a is NaN or null, undefined');
} else {
    // business logic ;)
}

To handle null also, caz isNaN(null) // false

AlexanderFSP
  • 652
  • 4
  • 10
  • You correctly use `a == null` instead of `a === null` here to check for both `null` and `undefined`. Nice. – Bart Hofland Mar 30 '20 at 08:49
  • `a == null` is impossible working in a team, one might think I forgot it and add a `=` this is not readable at all. Moreover tslint might reject it. – Rolintocour Apr 01 '20 at 09:58
  • @Rolintocour, with default angular tslint configuration it works well. Otherwise, you should do smth like this: `a === null || a === undefined'. But I prefer to make it shorter. – AlexanderFSP Apr 01 '20 at 10:03
3

You can use isNan. It will return true for undefined and NAN value but not for ZERO.

const variable = undefined;

 if(isNaN(variable)){
   console.log('I am undefined / NAN'); 
 }else{
   console.log('I am something / zero');
}
uiTeam324
  • 1,215
  • 15
  • 34
  • You SHOULD use `isNan()`. ;) You cannot directly use `NaN` in comparisons. The expression `NaN === NaN` will always return `false`. – Bart Hofland Mar 30 '20 at 08:45
  • 1
    In typescript, the `isNaN()` function does not accept the `undefined` argument. The error message is: `Argument of type 'undefined' is not assignable to parameter of type 'number'.` – Stephane Nov 28 '20 at 09:57
  • 1
    @Stephane, use isNaN(Number(YOURVARIABLEHERE)) – scott.korin Mar 09 '22 at 18:27
1

The correct way to check for undefined, null, NaN is

if(a == null || Number.isNaN(a)) {
  // we did it!
}

please notice the use of Number.isNaN instead of just isNaN. If you did use just isNaN then your check will not do what you want for values like strings or empty object

if(a == null || isNaN(a)) {
  // {} comes in because `isNaN({})` is `true`
  // strings that can't be coerced into numbers 
  // some other funny things
}

Read more at MDN

Jose V
  • 1,655
  • 1
  • 17
  • 31