4 ms·
Justifiable and terrible answers: - "" (the 1 gets converted to a string, and string-string could have meant remove-if-tail) - NaN or undefined (bad operation
by Strilanc 12y ago
Justifiable and terrible answers:
- "" (the 1 gets converted to a string, and string-string could have meant remove-if-tail)
- NaN or undefined (bad operations could give bad results)
- "1" (bad operations could be ignored)
- "1-1" (because life is hilarious, and a-b = a+(-b))
- Parse failure (the -1 could get parsed as a constant because there's no string minus int operation, and two adjacent constants is invalid)
- throws (bad operations could act like 'undefined()' does)
- MichaelGG 12y agoWhat's terrible about throwing in such a case? Edit: Apart from the terrible behaviors of browsers to just silently ignore all errors.
- Strilanc 12y agoSome of them are justifiable, some are terrible, and some are both. That's one of the justifiable ones.
- timepiece 12y agoI really like this "1-1" answer. That's very creative :) But honestly, compare each one of these answers to js's official workaround calling the Number() constructor on the primitive string value and attempting to convert to a number since the subtraction operator is only defined for number and then proceeding to complete the operation. For me, it looks very logical and convincing given that the language is loosely typed and it breaks her heart that it fails any of its loved users :)