I have an array of numbers. The array may contain a zero.
How can I check if the array contains a number or not? I am currently doing that:
if (Number(arr. value(pos)) != false)
This however, considers 0
as false too. So if a 0 is in the array, it will consider it as if the array was empty.
How can I overcome this?
Try this:
if (Number(arr. value(pos)) !== false)
!=
does a "falsy" check. 0, undefined, false, null, '',
and NaN
are all falsy. Everything else is "truthy". If you want to test for false only (and not all other falsy statements) you have to use ===
or !==
. Try to avoid !=
and ==
, because most if the time you actually mean !==
and ===
koenp's answer is perfectly correct, but if (!isNaN(arr. value(pos)))
might be preferable in this case. Also, it may be a notch faster. http://www.w3schools.com/jsref/jsref_isnan.asp
might be a solution. It checks whether the Number is bigger than Zero.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.