OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Glen Thompson
  • Start date Start date
G

Glen Thompson

Guest
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.

Code:
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.

<p>What is the most convenient / performant way to check if a variable is greater than or equal to 0 <strong>and</strong> not <code>null</code> or <code>undefined</code> or <code>''</code>? e.g. <code>[0, Infinity)</code>. Let's say my variable is <code>x</code>, the obvious <code>x >= 0</code> doesn't work as <code>null >= 0</code> is <code>true</code>. The best I could come up with was.</p>
<pre><code>x > 0 || x === 0
</code></pre>
<p>Can anyone think of a better one?</p>
<p>It seems like this question should already exist so if someone can find this question I'm happy to delete mine.</p>
 

Latest posts

Top