what is NaN, Object or primitive?
NaN - Not a Number
Description. NaN is a property of the global object. In other words, it is a variable in global scope. The initial value of NaN is Not-A-Number — the same as the value of Number.
In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties.
In JavaScript, NaN is a number that is not a legal number. The Number. isNaN() method returns true if the value is NaN , and the type is a Number.
JavaScript has the primitive types: number , string , boolean , null , undefined , symbol and bigint and a complex type: object .
It's a primitive. You can check in a number of ways:
typeof NaN
gives "number," not "object."
Add a property, it disappears. NaN.foo = "hi"; console.log(NaN.foo) // undefined
NaN instanceof Number
gives false (but we know it's a number, so it must be a primitive).
It wouldn't really make sense for NaN to be an object, because expressions like 0 / 0
need to result in NaN
, and math operations always result in primitives. Having NaN as an object would also mean it could not act as a falsey value, which it does in some cases.
NaN
is a primitive Number value. Just like 1
, 2
, etc.
NaN is a property of the global object.
The initial value of NaN is Not-A-Number — the same as the value of Number.NaN. In modern browsers, NaN is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it.
It is rather rare to use NaN in a program. It is the returned value when Math functions fail (Math.sqrt(-1)) or when a function trying to parse a number fails (parseInt("blabla")).
Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With