Why are "NaN" and "Undefined" not reserved keywords in Javascript?

We can do it:

NaN = 'foo' 

as well as

 undefined = 'foo' 

Why aren't they reserved keywords?

Edit 1 (DownVoters):

  • I think this should be implemented in order to be sure that when we look for number , it is number :)

  • If we must use IsNaN() or typeof , then why are NaN or undefined needed?

+15
javascript keyword
Aug 24 '11 at 10:14
source share
4 answers

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/NaN

NaN is a property of a global object.

The initial value of NaN is Not-A-Number - the same as the value of Number.NaN. In modern browsers, NaN is an unconfigurable, unprivileged property. Even if it is not, avoid overriding it.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined

undefined is a property of a global object, i.e. is a variable in a global area.

The initial value of undefined is the primitive value of undefined.

+7
Aug 24 2018-11-11T00:
source share

I cannot tell you why, but undefined and NaN are actually properties of a global object:

15.1.1 Properties of a global object property

15.1.1.1 NaN
Value NaN NaN ( see 8.5 ). This property has the attributes {[[Writable]]: false , [[Enumerable]]: false , [[Configurable]]: false }.
(...)
15.1.1.3 undefined
The value is undefined undefined ( see 8.1 ). This property has the attributes {[[Writable]]: false , [[Enumerable]]: false , [[Configurable]]: false }.

There is a difference between the value of undefined ( NaN ) and the corresponding property.

You may notice [[Writable]]: false . I'm not sure if this is new in ES5 (and cannot be adapted by all browsers), but in new browsers (tested in Firefox 6) assigning a new value of undefined has no effect:

 [12:28:15.090] < undefined = "foo" [12:28:15.093] > "foo" [12:28:19.882] < undefined [12:28:19.883] > undefined 

So, although you can assign a new value, you really cannot .




Why aren't they reserved keywords?

Not sure if there was any specific reason not to create reserved keywords, but it was decided against it. The language is still working. You cannot override these values, so this is normal.

The only way to check if the number is NaN is to use isNaN() anyway.

+8
Aug 24 '11 at 10:19
source share

I am thinking now, but the reason I think NaN and undefined are not keywords is because you usually don't assign these values ​​to variables.

 var x = undefined; // doesn't make sense, use null! var y = NaN; // You can't do much with this variable! 

undefined basically means uninitialized , and if you want to make it clear that the value is unknown, you use null . Thus, undefined usually means not initialized or the result of incorrect JavaScript code.

NaN rarely assigned manually, simply because you cannot do much with this value. This is usually the result of an erroneous calculation. The creators of JavaScript probably did not want to give this value the appearance of primitive values.

In addition, NaN also present in other languages, and it is not used as a keyword. For example: In C# NaN, Double.NaN is represented, since you do not distinguish between floating point and integer values ​​in JavaScript, I assume that why they put NaN using global identifiers!

Hope this clarifies the situation!

+3
Dec 10
source share

NaN not a keyword, but rather a built-in property of a global object and as such can be replaced (for example, undefined , but unlike the this keyword or the literals true , false and null ).

You can check if the value of NaN matches with the isNaN() function. Moreover, NaN is defined as not equal to everything, including itself.

Or in a nut shell you can say that:

NaN is the value returned when trying to process something that is not a number as a number. For example, the results 7 times "abc" are not a number. Its old form is Number.NaN. You can check the not-a-number values ​​with the isNaN() function.

Hope this helps.

+1
Aug 24 '11 at 10:19
source share



All Articles