Why does assigning "NaN" or "undefined" raise a TypeError?

AS: Question about the type of error, not about the phenomenon

"use strict" throws a TypeError if system variables such as NaN and undefined have changed.
But why is this a Type error? Why Not a Syntax Error?


Change Indeed, this is not a SyntaxError here, as there are no errors in the fragment syntax.
But the root of the error is that some protected object cannot be changed manually; therefore, it is lcely an Access error (there are none, I know).
Then, why are access errors displayed as types?

+7
javascript typeerror
source share
4 answers

There are 6 different types of native errors in ES5:

  • EvalError

    This exception is not currently used in this specification. This object remains compatible with previous versions of this specification.

  • RangeError

    Indicates that a numerical value has exceeded the allowable range.

  • ReferenceError

    Indicates that an invalid reference value has been detected.

  • Syntax Error

    Indicates that a parsing error has occurred.

  • TypeError

    Indicates that the actual type of the operand is different from the expected type.

  • Urieror

    Indicates that one of the global URI processing functions was used in which it is incompatible with its definition.

In your case, an error occurs because you are trying to assign the value window.NaN or window.undefined , which are not applicable.

Before assigning a new value, the [[Put]] internal method checks [[CanPut]] , which will return false because the property is not enumerable. And so [[Put]] will throw.

Thus, the problem is that the writability of the assigned link (the left operand in the expression ) is not expected. Then, among the 6 errors listed above, TypeError seems to be the most suitable, even if the written feature is not exactly type .

+4
source share

Because NaN and undefined are read-only window properties . They are not operators (e.g. > or + ), or keywords (e.g. var or class ).

Trying to assign something to NaN gives the following error in Chrome:

TypeError: Cannot set read-only property ' NaN ' of object ' #<Window> '

+2
source share

Because it is a valid argument that does not make sense. But this does not make sense at the SEMANTIC level, and not at the syntax.

Here is an example:

 1+1=42 

This is wrong, but it is wrong in a different way than, say:

 42 1 1 + = 

You can read the first as an arithmetic equation (one plus one equals forty two). Second, you cannot even read.

The same thing happens here. NaN=300; operator NaN=300; can be read in Javascript as "have NaN indicates a value of 300. But when the parser sends this request to the engine, the engine goes" No, can't do it. "

+1
source share

syntax error
In most programming languages ​​and scripts, this usually means that you used the statement in the wrong place.
For more information about syntax errors, see this article: https://en.wikipedia.org/wiki/Syntax_error

type error
This happens when skipping a data type meets some requirement.
For more information about data types, see this article: https://en.wikipedia.org/wiki/Data_type

Thus, a type error occurs because NaN and undefined are "data types" that are global constants with a specific (assigned) value, and "strict mode" prevents problems by not allowing these to be changed.

0
source share

All Articles