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.

7
  • undefined and NaN are both falsy values, so you can use if (variable) { } Commented Mar 30, 2020 at 8:03
  • 1
    Does this answer your question? Is there a way to check for both `null` and `undefined`? Commented Mar 30, 2020 at 8:05
  • @JakubJanik, it will return false for 0 also, which is not desired here. Commented Mar 30, 2020 at 8:09
  • 1
    FYI, variable !== NaN will always be true, because NaN != NaN. To properly check for NaN you need to use isNaN(). Commented Mar 30, 2020 at 8: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. Commented Mar 30, 2020 at 8:52

3 Answers 3

6
const a = 'value';

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

Handle null in addition, because isNaN(null) === false.

Sign up to request clarification or add additional context in comments.

3 Comments

You correctly use a == null instead of a === null here to check for both null and undefined. Nice.
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, 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.
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');
}

3 Comments

You SHOULD use isNan(). ;) You cannot directly use NaN in comparisons. The expression NaN === NaN will always return false.
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, use isNaN(Number(YOURVARIABLEHERE))
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.