I run the following code in a browser console (FireFox and Chromium):
console.log('v' > 5);
console.log('v' < 5);
Both statements return false. It actually comes as no surprise in case of 'v' < 5
, but why does 'v' > 5
return false? As far as I understand 5 gets converted to a string that is compared to 'v'
lexicographically. Did I missed something, what coercion rule is applied here?
The coercion is in the other direction. 'v'
is coerced to number, yielding NaN
, which will make any comparison with another number return false
.
See "What is the rationale for all comparisons returning false for IEEE754 NaN values?" on the behaviour of NaN
From the EcmaScript specification:
In 12.9.3 Runtime Semantics: Evaluation the evaluation of both <
and >
operators is specified, with this important step:
- Let r be the result of performing Abstract Relational Comparison rval < lval with LeftFirst equal to
false
.
And,
- If r is
undefined
, returnfalse
. Otherwise, return r.
The 7.2.11 Abstract Relational Comparison starts out with:
The comparison x < y, where x and y are values, produces
true
,false
, orundefined
(which indicates that at least one operand isNaN
).
NB: Note that undefined
will lead to a false
in the final evaluation, as stated in above quoted step 8 of section 12.9.3.
It is then required that after the primitive values have been taken from the operands, and they are not found to be both strings, they should be coerced to Number:
- If both px and py are Strings, then
[...]- Else
a. Let nx be ToNumber(px).
[...]
c. Let ny be ToNumber(py).
Here is a series of comparisons, showing the different outcomes you can get:
function test(value, name) {
if (arguments.length === 1) name = JSON.stringify(value);
console.log(name + ' < 11.5 === ' + (value < 11.5) +
'. Number(' + name + ') = ', Number(value));
}
test('33');
test('3');
test('+11.9'); // coerces to number 11.9 (sign and decimal)
test('0xA'); // coerces to number 10 (hexadecimal notation)
test(''); // coerces to number 0
test('3v'); // invalid number
test('3e2'); // coerces to number 300 (exponent notation)
test('-Infinity'); // coerces to number -Infinity
test(new Date(), 'new Date()'); // coerces to number of milliseconds
test({ valueOf: x => 2 }, '{ valueOf: x => 2 }'); // coerces to number 2
.as-console-wrapper { max-height: 100% !important; top: 0; }