Oliver Kane
Oliver Kane

Reputation: 898

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

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.

Upvotes: 1

Views: 743

Answers (2)

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28588

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.

Upvotes: 1

georg
georg

Reputation: 215039

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

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

Upvotes: 7

Related Questions