Why Number ('') returns 0, while parseInt ('') returns NaN

I looked at similar questions and answers on StackOverflow and found this:

parseInt("123hui") returns 123 Number("123hui") returns NaN 

As, parseInt() parses to the first non-digital number and returns everything that was parsed, and Number() tries to convert the entire string to a number, which is why the unlikely behavior is in the case of parseInt('') and Number('') .

I feel that ideally parseInt should return NaN just like using Number("123hui")

Now my next question is:

As 0 == '' returns true , I believe that it is interpreted as 0 == Number('') , which is true. So does the compiler really treat it like 0 == Number('') and don't like 0 == parseInt('') , or am I missing some points?

+8
javascript
source share
3 answers

The difference is partially related to Number() using additional logic to enforce the type. Included in the rules that he follows for this:

  • A StringNumericLiteral that is empty or contains only a space is converted to +0 .

While parseInt() is defined simply for searching and evaluating numeric characters at the input based on a given or detected radix . And it was determined that at least one valid character is expected.

13) If S contains a code block that is not a radix-R digit, let Z be a substring of S consisting of all code blocks up to the first such code block; otherwise let Z - S.

14) If Z is empty, return NaN .

Note: "S" is the input string after removing any leading space.


How 0=='' returns true I believe that it is interpreted as 0==Number('') [...]

The rules used == are defined as Abstract Equality .

And you are right about the coercion / transformation used. Corresponding step: # 6:

If Type (x) is Number and Type (y) is String,
return the result of the comparison x == ToNumber (y).

+4
source share

To answer your question about 0=='' returning true:

The following is a comparison of a number and a string:

Equals operator (==)

 Type (x) Type(y) Result ------------------------------------------- x and y are the same type Strict Equality (===) Algorithm Number String x == toNumber(y) 

and toNumber executes the following string arguments:

 toNumber: Argument type Result ------------------------ String In effect evaluates Number(string) "abc" -> NaN "123" -> 123 

Number('') returns 0 . This way you get 0==0 , which is evaluated using the Strict Equality algorithm ( === ))

Strict Equals Operator (===)

 Type values Result ---------------------------------------------------------- Number x same value as y true (but not NaN) 

You can find the complete list @ javascriptweblog.wordpress.com - truth-equality and-javascript .

0
source share

parseInt("") is NaN because the standard says so, even if +"" is 0 instead (also just because the standard says so, meaning, for example, that "" == 0 ).

Do not look at the logic in this, because there is no deep deep logic, just history .

You, in my opinion, are making a BIG mistake ... the sooner you fix it, the better it will be for your life to program using Javascript. The mistake is that you assume that every choice made in programming languages ​​and every technical information about them is logical. This is simply not true.

Especially for Javascript.

Please remember that Javascript was “designed” in a hurry and, simply because of fate, it became extremely popular overnight. This forced the community to standardize it before a serious thought about the details, and therefore it was basically “frozen” in its current sad state before serious field trials.

There are things that are so bad that they aren’t even funny (for example, the with operator or the equality operator == , which is so broken that serious js-IDE warn about using any : you get things like A==B , B==C and A!=C , even using only ordinary values ​​and without any "special" value , for example null , undefined , NaN or empty lines "" and not because of precision tasks).

Honest special cases are everywhere in Javascript, and trying to put them in a logical frame is, unfortunately, wasted. Just study its oddities, read a lot and enjoy the fantastic runtime environment it provides (this is where Javascript really shines ... browsers and their JITs are truly an impressive piece of technology: you can write a few lines and get really useful software on a gajillion different computing devices).

The official standard, which lists all the oddities, is quite difficult to read, because it is very accurate, and, unfortunately, the rules that it must indicate are really complex.

Moreover, as the language gets more features, the rules will become more and more complex: for example, for ES5, just another strange “special” case (for example, ToPrimitive behavior for Date objects) becomes the “normal” case in ES6 (where ToPrimitive can be customized).

Not sure if this “normalization” is something to be happy about ... the real problem is the frozen starting point, and there are no easy solutions right now (if you don't want to throw away all existing javascript code, that is).

The normal path for the language begins clean and pleasant, symmetrical and small. Then, when confronted with real-world problems, the language receives (gets infected) some ugly parts (because the world is ugly and asymmetric).

Javascript is like that. Besides the fact that he did not become pleasant and clean, and, in addition, there was no time to polish him before throwing him into the game.

-one
source share

All Articles