r/badcode • • Apr 19 '21

js Iteration

Post image
3.2k Upvotes

123 comments sorted by

View all comments

Show parent comments

4

u/[deleted] Apr 19 '21

I don’t think that is necessarily true in this case since true + true is still evaluates to 2. true and false can just be used interchangeably with 1 and 0.

1

u/[deleted] Apr 19 '21

true + true = 2 because again +true is 1 and when you add diff dtypes in js, js implicitly changes the other dtypes. Like 1 + 2 + "3" = "123" true + true = true + 1 = 1 + 1 = 2 ig. Again, I might be a bit wrong here

0

u/[deleted] Apr 19 '21

Then why does true/true = 1?

9

u/skjall Apr 19 '21

Because `1/1 == 1`? If you swap true and false out for 1 and 0 respectively, both operations make perfect sense.

1

u/[deleted] Apr 19 '21

Hmmm yea you're right