Due to the fact that adding booleans makes no sense, JS casts all those "true"s into numbers, and because "true" is usually stored as a 1, every true is casted into a 1, and then added.
11) true - true = 0
Again, implicit casting.
12) true == 1
In JS, when you compare two values with different types with "==", you cast one of the values to the corresponding type. As I said earlier, true is casted into a 1, and therefore, this happens.
13) true === 1
The triple equal means that if they're of different type, it will return false, and because a boolean is not a number, it returns false.
14) (!+[]+[]+![]).length == 9
Yeah... Again, I don't know...
15) 9 + "1" = "91"
The sign + means either addition with numbers, or concatenation with strings. Because they're of different type, one has to be casted from a type to the other. In this case, JS casts the number into a string and then concatenates.
16) 91 - "1" = 90
Due to the fact that the sign - means nothing with strings, JS HAS to cast the string to number for that to make any sense, so "1" becomes 1, and 91-1 is 90.
17) [] == 0
Again, implicit casting.
edit: forgot that on reddit you have to line break twice for that line break to be shown once.
Math.max = function () {
var buffer = -Infinity;
for (var i = 0; i < arguments.length; i++) {
if(arguments[i]>buffer){
buffer=arguments[i];
}
}
return buffer;
}
Math.max() starts with -Infinity (the smallest possible value) and then loops over all arguments, comparing them and replacing if newValue>existingValue
Since there are no arguments, nothing ever gets compared to -Infinity. Math.min() works this way too but goes the other direction.
Also, it's the most "neutral" result (there's probably a better term for this), like how in mathematics an empty sum is defined to equal 0, or an empty product to equal 1. It has nice properties like not breaking max(max(X), max(Y)) if X or Y are empty.
The questionable part here is that casting [] to boolean converts it to true even though casting it to integer or string converts it to 0 or "" respectively, which are both falsey.
Actually, the really strange part is that ![] === false. Because [] == false, keeping it in line with 0 and "" (it's number and string representations). But that means that [] == false == ![].
Ah, that's entirely reasonable...or like 30% reasonable. I'd feel better about it if JavaScript had operator overloading, but I don't want to live in a world where JavaScript has operator overloading.
That's sort of JavaScript's original sin, though. Pretty much all of the horrible things in JavaScript derive from its attempts to just truck past anything a sane language would throw an error about.
It's not so much an issue of static typing, as it is one of JS's original design goals to try to never crash.
Ruby, for example, isn't statically typed, but will quite happily throw exceptions if you try to do things like 1 + "1" or even just "foo" + :bar.
(although it can do implicit conversion if you define the appropriate methods, e.g. to_str(), but thankfully the core classes have sensible definitions)
First of all, why would you call this function without parameters? Secondly, it actually makes some sense. It basically tries to find maximum value in empty set, which does not exists so it returns lowest possible value.
Math.max() is supposed to return the max of the numbers provided as arguments, (not the maximum value of that type). So when it’s provided no arguments it returns the smallest possible value
View all comments
690
u/hectobreak Jun 21 '18 edited Jun 21 '18
Ok, here we go:
That is not because of JavaScript, but because of the IEEE 754 standard for floating point numbers.
2) [large number] = [another large number]
This one is, again, due to the floating point standard.
3) and 4)
Again, floating point. This time it's a floating point rounding error.
5) and 6)
yeah... I've got nothing.
7), 8) and 9)
http://2ality.com/2012/01/object-plus-object.html
10) true + true + true = 3
Due to the fact that adding booleans makes no sense, JS casts all those "true"s into numbers, and because "true" is usually stored as a 1, every true is casted into a 1, and then added.
11) true - true = 0
Again, implicit casting.
12) true == 1
In JS, when you compare two values with different types with "==", you cast one of the values to the corresponding type. As I said earlier, true is casted into a 1, and therefore, this happens.
13) true === 1
The triple equal means that if they're of different type, it will return false, and because a boolean is not a number, it returns false.
14) (!+[]+[]+![]).length == 9
Yeah... Again, I don't know...
15) 9 + "1" = "91"
The sign + means either addition with numbers, or concatenation with strings. Because they're of different type, one has to be casted from a type to the other. In this case, JS casts the number into a string and then concatenates.
16) 91 - "1" = 90
Due to the fact that the sign - means nothing with strings, JS HAS to cast the string to number for that to make any sense, so "1" becomes 1, and 91-1 is 90.
17) [] == 0
Again, implicit casting.
edit: forgot that on reddit you have to line break twice for that line break to be shown once.