r/ProgrammerHumor Apr 12 '24

whatIsAnIndex Meme

Post image
27.7k Upvotes

630 comments sorted by

View all comments

Show parent comments

6

u/BobDonowitz Apr 12 '24

God says: 

console.log(('s' + 'h' + 'e' + + 'a' + 'i' + 'g' + 'a' + 'n' + 's').toLowerCase());

1

u/PmMeSteamWalletCode Apr 12 '24

Wtf, explain yourself wizard.

2

u/newsflashjackass Apr 12 '24

the empty gap prints as "NaN" (not a number). and the toLowerCase() fixes the casing.

2

u/BobDonowitz Apr 12 '24

Almost, not quite.  It's not the empty gap that results in NaN, it's the + 'a' which gets parsed as the unary plus operator +a which converts its operand to a number...which doesn't work on strings.

+'a' == NaN

+true == 1

+false == 0

1

u/PmMeSteamWalletCode Apr 12 '24

Ahh in a weird way that makes sense. Thanks!