Why is isFinite(undefined) != isFinite(null)?

旧巷老猫 提交于 2019-12-13 05:42:08

问题


Why is the value for undefined considered Finite in javascript while null is not?

This is a very basic question, which has thwarted my googlefoo (too much noise).

isFinite(undefined); // false
isFinite(null); // true

I do not understand as I would expect null and undefined to be handled in the same manner.


回答1:


This is because Number(null) === 0.

http://es5.github.io/#x9.3




回答2:


isFinite (number)

Returns false if the argument coerces to NaN, +∞, or −∞, and otherwise returns true.

isFinite convert input using Number() and:

Number(undefined); //== NaN
Number(null); //== 0

that is the reason undefined is false and null is true for isFinite.

If you try:

isFinite(!undefined); // true

because undefined is NaN and on negating it it converted to 1 which is finite.



来源:https://stackoverflow.com/questions/19254205/why-is-isfiniteundefined-isfinitenull

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!