r/ProgrammerHumor • • Jun 21 '18

Thanks Brendan for giving us the Javascript

Post image
3.1k Upvotes

302 comments sorted by

View all comments

690

u/hectobreak Jun 21 '18 edited Jun 21 '18

Ok, here we go:

  1. "typeof NaN is number"

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.

249

u/AyrA_ch Jun 21 '18

5+6:

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.

80

u/Kwantuum Jun 21 '18

I was really confused by this one because I thought it was float_max when it's actually args_max. Makes much more sense now.

31

u/AquaWolfGuy Jun 21 '18

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.

31

u/once-and-again ☣️ Jun 21 '18

(there's probably a better term for this)

The customary term is identity element: e.g., -Infinity is the identity element for the max operation.

6

u/AyrA_ch Jun 21 '18

if X or Y are empty.

Be careful though because Math.max(undefined) is NaN since it tries to convert arguments to numbers. It also suffers from NaN poisoning.

13

u/ILikeLenexa Jun 21 '18

Ahh, I assumed Math.max() was equivalent to java's Integer.maxValue()

10

u/AyrA_ch Jun 21 '18

JS doesn't has real integers most of the time so it provides these constants:

> [Number.MAX_VALUE, Number.MAX_SAFE_INTEGER, Number.MIN_VALUE, Number.MIN_SAFE_INTEGER]
< [1.7976931348623157e+308, 9007199254740991, 5e-324, -9007199254740991]

You can simulate 32bit integer behaviour by doing a binary OR with a number:

function add(a,b){return (a|0+b|0)|0;}

This would perform an integer sum

6

u/[deleted] Jun 21 '18

I think the maximum of the empty set is just defined to be -infinity, it makes some sense mathematically

→ More replies (1)
→ More replies (4)

121

u/funfact15 Jun 21 '18

14)!+[]+[]+![] is equal to "truefalse"
![] equals to true //bool
[]+![] equals to "false" //string

type+string is string

true + "false" is "truefalse"

"truefalse".length is 9

45

u/KeinBaum Jun 21 '18 edited Jun 21 '18

It's actually:

  • !+[] === !0 === true
  • true+[] === "true" + "" === "true"
  • "true"+![] === "true"+false === "truefalse"

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 == ![].

9

u/ILikeLenexa Jun 21 '18

Hang on a second. I'm way more concerned about why you can even add a raw ! to anything?

19

u/gopher_protocol Jun 21 '18

You're not, you're applying ! to +[], which is the unary "positive" operator applied to [], which converts it to 0. Then !0 === true.

18

u/ILikeLenexa Jun 21 '18

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.

10

u/venuswasaflytrap Jun 21 '18

Jesus Christ. JavaScript with operator overloading? You can already write js exclusively with operators.

→ More replies (10)

27

u/FisHBoNe19 Jun 21 '18

[] == false

![] == false // I don't know why

!+[] == true

8

u/tofiffe Jun 21 '18

This just raises further questions

→ More replies (3)
→ More replies (2)

54

u/remy_porter Jun 21 '18

Again, implicit casting

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.

0

u/[deleted] Jun 21 '18

[deleted] — view deleted comment

15

u/caerphoto Jun 21 '18

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)

→ More replies (3)
→ More replies (2)

26

u/hemenex Jun 21 '18

5) and 6)

Math.max() -> -Infinity

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.

→ More replies (8)

20

u/TigreDemon Jun 21 '18

Yeah, this one ^

Too many people not knowing === ... of course bool != number so it returns false.

0

u/[deleted] Jun 21 '18

[deleted] — view deleted comment

10

u/DannoHung Jun 21 '18

That's how you know you're on programmer humor though, the rationalizations are the real joke.

→ More replies (1)

5

u/GoldenWillie Jun 21 '18

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

→ More replies (47)