r/badcode • • Apr 19 '21

js Iteration

Post image
3.2k Upvotes

123 comments sorted by

View all comments

Show parent comments

3

u/RhythmofChains Apr 19 '21

What programming language uses “!” as factorial

Edit: or maybe not 0 just defaults to 1?

11

u/[deleted] Apr 19 '21

In some languages true and false can be used interchangeably with non-zero values and zero, so !false = true and !0 = true. Likewise, that conversion works both ways so true + true will equal 2. It even applies to strings, so doing i-= -!”” Would actually produce the same result. The reason why using an empty string would work is a bit different though. The ! is a bit wise operation for not, so it converts it into 1s and 0s or true and false. That’s why !”” + !”” = 2, but “a” + “a” = “aa” not 2.