Glen Thompson
Glen Thompson

Reputation: 9996

Javascript check >= 0 not null or undefined or ''

What is the most convenient / performant way to check if a variable is greater than or equal to 0 and not null or undefined or ''? e.g. [0, Infinity). Let's say my variable is x, the obvious x >= 0 doesn't work as null >= 0 is true. The best I could come up with was.

x > 0 || x === 0

Can anyone think of a better one?

It seems like this question should already exist so if someone can find this question I'm happy to delete mine.

Upvotes: 2

Views: 9000

Answers (6)

KooiInc
KooiInc

Reputation: 122906

I created a small module to check the type of (nearly) anything in ECMAScript. With it, checking if a variable is a Number, it returns true if and only if it is indeed a Number. Maybe useful. Here's a small example:

const { IS } = TOAFactory();
const numberGT0 = n => IS(n, Number) && n >= 0;
// ^ the actual check

const stringify = s => IS(s, String) ? 
  `"${s}"` : 
  IS(s, null, undefined, NaN, Infinity) ? String(s) : 
  JSON.stringify(s);
  
// a number of values to check
const values = [
  null, NaN, undefined, 1/0, `42`, '', {a:0}, [0], 42,
];

values.forEach( v => console.log( `${stringify(v)} >= 0? ${
  numberGT0(v) ? "YEP" : "NOPE"} (typeof v = ${typeof v})` ) );
.as-console-wrapper {
    max-height: 100% !important;
}
<script 
  src="https://kooiinc.github.io/typeofAnything/typeofany.browser-factory.js">
</script>

Note One ES peculiarity I know of can't be catched with this (maybe there are more):

  • x = true + false // => 1.

Upvotes: 0

customcommander
customcommander

Reputation: 18901

Perhaps not the answer you're looking for but why not check for number first?

if (typeof x === "number" && x >= 0) {
  // Excludes non-number values such as null and undefined
}

Note that this will allow Infinity, if you want finite numbers only:

if (typeof x === "number" && isFinite(x) && x >= 0) {
  // ...
}

The number type check clarifies your intent.

Any attempt to make it short and smart will most likely rely on type coercion which is risky, complicated (not everybody is familiar with JS quirks) and unnecessary from a performance point of view.

Upvotes: 7

JohnnyPro
JohnnyPro

Reputation: 143

Try this:

if (x != null && x >= 0)
{
    //then insert the code to be executed here
}

Upvotes: 0

kasperhoffmann
kasperhoffmann

Reputation: 1

The not null call could be as follows

value !== null

And i think you should just keep the above zero check your already doing!

Upvotes: 0

Justinas
Justinas

Reputation: 43481

Use non-strict comparison with null and then compare numbers:

function check(x) {
  return x != null && 0 <= x;
}

console.log(check(1), true);
console.log(check(0), true);
console.log(check(-1), false);
console.log(check(null), false);
console.log(check(undefined), false);

console.log(check('1'), true);
console.log(check('0'), true);
console.log(check('Hello'), false);
console.log(check('-1 and 2 and 3'), false);

Upvotes: 1

Ketan Vekariya
Ketan Vekariya

Reputation: 352

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.

Further read: http://typeofnan.blogspot.com/2011/01/typeof-is-fast.html

Upvotes: -1

Related Questions