Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does assigning to `NaN` or `undefined` cause a TypeError?

A.S.: The question is about type of error, not about the phenomenon

"use strict" throws TypeError if system variables like NaN and undefined got changed.
But why is it a TypeError? Why not SyntaxError?


Edit: Really, it is not the SyntaxError here since there are no errors in syntax of the snippet.
But the root of the error lies in the fact, that some protected object cannot be changed manually; so, it is lkely the AccessError (there are no such, I know).
Then, why access-errors appear like type-ones?

like image 297
Dima Parzhitsky Avatar asked May 08 '16 18:05

Dima Parzhitsky


People also ask

How do you fix NaN errors?

Nan means “Not a number”, this is because inside your cube function, you're not calling the square function, but getting it's contents. Change return x * square; with return x * square(x); and it should work.

Why do we get NaN error?

NaN means not a number. Despite the name, JavaScript still considers it a number. If returns 'number' . It's returned whenever we try to convert something that doesn't have numeric content into a number with the Number function.

What does NaN undefined mean?

NaN (Not a Number) is a numeric data type that means an undefined value or value that cannot be represented, especially results of floating-point calculations.

Is NaN and undefined the same?

The undefined property indicates that the variable has not been assigned a value or not declared at all. The NaN property represents a “Not-a-Number” value. The NaN property indicates that a value is not a legitimate number.


3 Answers

In ES5, there are 6 different kinds of native errors:

  • EvalError

    This exception is not currently used within this specification. This object remains for compatibility with previous editions of this specification.

  • RangeError

    Indicates a numeric value has exceeded the allowable range.

  • ReferenceError

    Indicate that an invalid reference value has been detected.

  • SyntaxError

    Indicates that a parsing error has occurred.

  • TypeError

    Indicates the actual type of an operand is different than the expected type.

  • URIError

    Indicates that one of the global URI handling functions was used in a way that is incompatible with its definition.

In your case, the error is thrown because you attempt to assign a value to window.NaN or window.undefined, which are non-writable properties.

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

So the problem is that the writability of the assigned reference (left operand in the assignment expression) is not the expected one. Then, among the 6 error kinds above, the most appropriate seems TypeError, even if writability is not exactly a type.

like image 193
Oriol Avatar answered Sep 20 '22 13:09

Oriol


Because NaN and undefined are just read only properties of window. They are not operators (like > or +), or keywords (like var or class).

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

TypeError: Cannot assign to read only property 'NaN' of object '#<Window>'

like image 32
Michał Perłakowski Avatar answered Sep 20 '22 13:09

Michał Perłakowski


Because it's a valid argument that does not make sense. But it does not make sense in the SEMANTIC level, not the syntactic.

Here's an example:

1+1=42

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

42 1 1 + =

You can read the first one as an arithmetic equation (one plus one equals forty-two). The second one, you can't even read.

The same goes here. the statement NaN=300; can be read in Javascript as "have NaN point to the value 300`. But when the parser submit this request to the engine, the engine goes "Nope, can't do this".

like image 44
Michael Bar-Sinai Avatar answered Sep 20 '22 13:09

Michael Bar-Sinai