I have var a;
Its value may be NaN, null and any +ve/-ve number including 0.
NaN, null and any +ve/-ve number including 0.
I need a condition that filters out all the values โโof a, so that only values> = 0 give the true value in the if condition.
What is the best way to achieve this, I don't want to use 3 different conditions related to using ||
||
Oh ... But I really found ans .. it's that simple.
parseInt (null) = NaN.
So if(parseInt(a)>=0){} will do ... Yayyee
if(parseInt(a)>=0){}
typeof x == "number" && x >= 0
This works as follows:
null
typeof null == "object"
NaN
typeof NaN == "number"
number
I had the same problem a few weeks ago, I solved it with:
if(~~Number(test1)>0) { //... }
http://jsfiddle.net/pT7pp/2/
This works well:
if (parseFloat(x) === Math.sqrt(x*x))...
Test:
isPositive = function(x) { return parseFloat(x) === Math.sqrt(x*x) } a = [null, +"xx", -100, 0, 100] a.forEach(function(x) { console.log(x, isPositive(x))})
NaN not >= 0 , so the only exception you need to make is null :
>= 0
if (a !== null && a >= 0) { ... }
My best solution to filter these values โโwould be 2 conditions, and it looks like:
if(a!=undefined && a>=0){ console.log('My variable is filtered out.') }
I am not sure, but there is no single condition for its use.
Since you marked jQuery, take a look at $.isNumeric()
$.isNumeric()
if($.isNumeric(a) && a >= 0)
Source: https://habr.com/ru/post/944693/More articles:How can I introduce a 5-tuple in RDF? - rdfthe application requires the assembly microsoft.reportviewer.processingObjectModel version 11.0.0.0 to be installed in the global assembly cache - .net-4.0Trapping baseline errors - backbone.jsProper use of the azure vault. (When to use SQL, Tables and Blobs) - azure-sql-databaseAndroid 2d, why libgdx instead of another - androiddoes backbone.js set callback before saving? - callbackScrolling up and down a given number of pixels using jQuery scrollTop - javascriptDeploy weblogic using maven and jenkins - mavenpassing file name to R from javascript using Rook package - javascriptHow to create all possible combinations of lines with spaces between characters? python - pythonAll Articles