3 ms·
Well I can answer that myself thanks to sheetjs' link to the spec. Case 1: From the section for 'if ()', http://www.ecma-international.org/ecma-262/5.1/#sec-12
by farnsworth 12y ago
Well I can answer that myself thanks to sheetjs' link to the spec.
Case 1: From the section for 'if ()', http://www.ecma-international.org/ecma-262/5.1/#sec-12.5 http://www.ecma-international.org/ecma-262/5.1/#sec-12.5,
If ToBoolean(GetValue(exprRef)) is true, ...
new Boolean(2) => true, so 2 is 'truthy' alone inside if ().
Case 2: From the == section, http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3,
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
So the boolean is coerced to a number, and new Number(true) => 1. I wonder why they didn't do it the other way around, and make the number a boolean like above. You're losing seemingly useful information with new Number(<boolean>)
- deleted 12y ago[deleted]