JS Unitary Plus Operator Behavior Applied to a String Representing Negative Hexadecimal

according to MDN when using the unary plus operator:

Integers in decimal and hexadecimal formats ("0x" -converted) are supported. Negative numbers are supported ( but not for hex ). If it cannot analyze a specific value, it will be evaluated to NaN.

But when I run this Jasmine test (a match toBe() applies the === operator):

  it("should return NaN when trying to convert a string representing a NEGATIVE HEX to the corresponding number", function() { var a = '-0xFF'; expect(typeof +a).toBe('number'); expect(isNaN(+a)).toBeTruthy(); //Fails on Chrome and Opera... }); 

It does not work in Chrome and Opera (and goes through IE, Safari and Firefox).

Is this a flaw in Chrome and Opera devices, or am I missing something?

+6
source share
2 answers

It may or may not be considered a flaw, depending on how it is tied to the specifications. )

I found an interesting discussion regarding this behavior. It seems that Firefox was once “better than speculating” in the camp, but then fixed it according to the specification.

+2
source

According to the EcmaScript specification, the unary + operator applies [String-] to-Number-conversion to a value (here is a string) that accepts hexadecimal numbers but not negative hexadecimal numbers.

+1
source

Source: https://habr.com/ru/post/914314/


All Articles